Monday, 6 October 2014

Find how many times a search phrase is found in a string 0

Here is the function to tell how many times a search phrase is found in a string.

Note: It's case sensitive.

function howMany(str, term){
return str.match(new RegExp(term, 'g')).length;
};

var text='Visit WHAK.com for WHAKy things!';
var phrase='HAK';
alert(phrase+' is found in\n'+text+'\n'+howMany(text,phrase)+' times.')

Thanks Christian White

0 comments:

Post a Comment