<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"> </script> </head> <body> <p>题目:请校验输入框内的字符含是否含有!@#$%^&*字符</p> 请输入题目规定的字符:<input type="text" id="in" /> <input type="button" id="btn" value="click"/> <script> $(function () { var truePwd = /[\!\@\#\$\%\^\&\*]/; $("#btn").click(function () { console.log( $("#in").val()); if(truePwd.test( $("#in").val())){ alert("含有字符: "+truePwd.exec( $("#in").val())); }else{ alert("匹配失败"); } }) }) </script> </body> </html>
运用方法:
Test():检测会否存在,返回值为true/false;
exec():检索指定的值,返回值是被找到的值,如果没有找到,返回null;