Javascript regular expression: () |
<input type="button" value="Match" onclick="javascript:demo1('tim says boehihi);return false;" /> <input type="button" value="No Match" onclick="javascript:demo1('tim says boehi');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo1(text) { var re = /tim says boe(hi){2}/i; if (text.match(re)) { alert("match"); } else { alert("no match"); } } //]]> </script> |
<!-- Use the special syntax (?:value) to group tokens without creating a capturing group. This is more efficient. --> <input type="button" value="Show1" onclick="javascript:demo2('webdesigner');return false;" /> <input type="button" value="Show2" onclick="javascript:demo2('web');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo2(text) { var re1 = /web(designer)?/; var re2 = /web(?:designer)?/; var found1 = text.match(re1); var found2 = text.match(re2); if (found1 != null && found1.length > 0) { for(i=0;i<found1.length;i++){ alert("found1[" + i + "]= " + found1[i]); } } if (found2 != null && found2.length > 0) { for(i=0;i<found2.length;i++){ alert("found2[" + i + "]= " + found2[i]); } } } //]]> </script> |