Quantcast
Channel: How to count string occurrence in string? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by N Djel Okoye for How to count string occurrence in string?

$
0
0

This function will tell you if the substring is in the string and how many times.

const wordInText = (wordToFind, wholeText) => {    const wordToFindRegex = new RegExp(wordToFind, 'gi');    const occurences = wholeText.match(wordToFindRegex) ?? [];    return {isWordInText: occurences.length > 0, occurences: occurences.length}; } console.log(wordInText("is", "This cow jumped over this moon"))

Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>