Javascript regular expression: . |
<input type="button" value="Match1" onclick="javascript:demo1('help');return false;" /> <input type="button" value="Match2" onclick="javascript:demo1('h lp');return false;" /> <input type="button" value="No Match1" onclick="javascript:demo1('hlp');return false;" /> <input type="button" value="No Match2" onclick="javascript:demo1('heelp');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo1(text) { var re = /h.lp/; if (text.match(re)) { alert("match"); } else { alert("no match"); } } //]]> </script> |
<!-- The repetition operators are greedy. They will expand the match as far as they can. Place a question mark after the quantifier to make it lazy. --> <input type="button" value="Show" onclick="javascript:demo2('the <b>green</b> hornet');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo2(text) { var re1 = /<.+>/; var re2 = /<.+?>/; 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> |