Skip to content

Instantly share code, notes, and snippets.

@TravelingTechGuy
Created June 16, 2014 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravelingTechGuy/79b030b2c5b094c3f6e7 to your computer and use it in GitHub Desktop.
Save TravelingTechGuy/79b030b2c5b094c3f6e7 to your computer and use it in GitHub Desktop.
Find number of occurrences of substring in string
var str = "This is a string.",
term = 'is',
reg = new RegExp(term, 'ig'); //remove the `i` if you need case-sensitivity
var count = str.match(reg).length;
console.log(count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment