Javascript regular expression: /g |
<input type="button" value="Found1" onclick="javascript:demo1('I like red and Fred has a red car.');return false;" /> <input type="button" value="Found2" onclick="javascript:demo1('Lured and liquidated.');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo1(text) { var re = /\bred\b/; var found = text.match(re); if (found != null && found.length > 0) { alert("Number found = " + found.length); alert("found.index = " + found.index); alert("found.input = " + found.input); for(i=0;i<found.length;i++){ alert("found[" + i + "]= " + found[i]); } } else { alert("no match"); } } //]]> </script> |
<input type="button" value="Found1" onclick="javascript:demo2('I like red and Fred has a red car.');return false;" /> <input type="button" value="Found2" onclick="javascript:demo2('Lured and liquidated.');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo2(text) { var re = /\bred\b/g; var found = text.match(re); if (found != null && found.length > 0) { alert("Number found = " + found.length); alert("found.index = " + found.index); alert("found.input = " + found.input); for(i=0;i<found.length;i++){ alert("found[" + i + "]= " + found[i]); } } else { alert("no match"); } } //]]> </script> |