Skip to main content
Inspiring
August 31, 2008
Question

regular expressing in javascript

  • August 31, 2008
  • 1 reply
  • 470 views
Hi All,

Does anybody know if there's a way to find out how many words appear in a string using regular expression in javascript? For example, I have the following code that pops a "Not OK" alert whenever str contains "it it it information technology", and I need to find out how many times the word "it" exists in the string using regular expression.

<html>
<body>

<script type="text/javascript">
var str = "it it it information technology";
var reg = /^(\bin\b|\bit\b|\bof\b)(?! (\bin\b|\bit\b|\bof\b))/;
reg = new RegExp(reg);
var result = str.match(reg);

document.write(result);
if (result) {
alert("OK");
} else {
alert("Not OK");
}
</script>

</body>
</html>

Thanks very much in advance!
This topic has been closed for replies.

1 reply

Inspiring
September 1, 2008
Refer this tutorial.
Inspiring
September 2, 2008
Thanks, Daverms. It helps.