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

Answer by Simm for How to count string occurrence in string?

$
0
0

I think the purpose for regex is much different from indexOf.indexOf simply find the occurance of a certain string while in regex you can use wildcards like [A-Z] which means it will find any capital character in the word without stating the actual character.

Example:

 var index = "This is a string".indexOf("is"); console.log(index); var length = "This is a string".match(/[a-z]/g).length; // where [a-z] is a regex wildcard expression thats why its slower console.log(length);

Viewing all articles
Browse latest Browse all 43

Trending Articles



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