Javascript regular expression: test() |
<input type="button" value="Match" onclick="javascript:demo('hello world');return false;" /> <input type="button" value="No Match" onclick="javascript:demo('hello Jack');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo(text) { var re = /world/; if (re.test(text)) { alert("match"); } else { alert("no match"); } } //]]> </script> |
<!-- Check valid url --> <input type="button" value="Match1" onclick="javascript:demo2('WWW.MOBILEFISH.COM');return false;" /> <input type="button" value="Match2" onclick="javascript:demo2('www.mobilefish.com/test');return false;" /> <input type="button" value="Match3" onclick="javascript:demo2('www.mobilefish.com/test.html#hello');return false;" /> <input type="button" value="Match4" onclick="javascript:demo2('www.mobilefish.com/0,2821,1859_270513,00.html');return false;" /> <input type="button" value="Match5" onclick="javascript:demo2('www.mobilefish.com/test.html?var=1&var2=2');return false;" /> <input type="button" value="No Match6" onclick="javascript:demo2('mobilefish');return false;" /> <input type="button" value="No Match7" onclick="javascript:demo2('www#.mobilefish');return false;" /> <script type="text/javascript" language="JavaScript"> //<![CDATA[ function demo2(text) { var re = /^[a-z0-9_-]+\.[a-z0-9-_%&\?\/\.=#,]+$/i; if (re.test(text)) { alert("match"); } else { alert("no match"); } } //]]> </script> |