function howMany(str, term){ return str.match(new RegExp(term, 'g')).length; }; var text='Visit WHAK.com for WHAKy things!'; var phrase='HAK'; out.innerHTML = phrase+' is found in\n'+text+'\n'+howMany(text,phrase)+' times.';