Quantcast
Viewing latest article 1
Browse Latest Browse All 43

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

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 latest article 1
Browse Latest Browse All 43

Trending Articles



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