Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

regular expressing in javascript

Contributor ,
Aug 30, 2008 Aug 30, 2008
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!
TOPICS
Getting started
404
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 01, 2008 Sep 01, 2008
Refer this tutorial.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 02, 2008 Sep 02, 2008
LATEST
Thanks, Daverms. It helps.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources