Hi,

I'm building a small script which needs to show the number of time a word appears in a textarea. So if you type this in the textarea box:

text text test text test
Then it should say that 'test' appeared twice. This works fine by doing something like this:

Code:
word='test';
arr=text.split(word);
count=arr.length - 1;
However, here's the problem. If I put in the following text:

text text tests test text text
Here, technicaly test appears twice, but it actually only appeared once (because the other time is was a part of 'tests' which is another word). I need to calculate this correctly, so it only shows the number of times test appeared as a whole word.

I've considered doing something like this:
Code:
var arr=content.split(word + ' ');
By doing this, it only matches if a space was typed right after 'test'. But if someone typed a question mark (?), exclamation mark (!), or any other punctuation character, it wouldn't match and that is also a problem.

Can someone help me figure out a way to find the number of times a word appeared as a whole in the text? May be something could be done with regexps..