日期:2014-05-16  浏览次数:20339 次

js正则表达式应用 方法
正则表达式方法:test(),exec()

例子:test
<html>
<head>
  <script language="Javascript1.2">
     <!-- start hiding
     function verify(obj)
     {
      var email = obj.mail.value;
          var pattern =/^.{3}$/; 
      flag = pattern.test(mail);
      if(flag)
      {
       alert("Your mail is correct!");
       return true;
      }
      else
       {
        alert("Please try again!");
        return false;
        }
      }
     // stop hiding -->
    </script>
  </head>
  <body>
   <form onSubmit="return verify(this);">
    <input name="mail" type="text">
    <input type="submit">
    </form>
  </body>
</html>

例子: exec()
<script type="text/javascript">
var reg=/test/;
var str='testString';
var result = reg.exec(str);
alert(result);
<script>


区别:
exec(string),对string进行正则处理,并返回匹配结果.
test(string),测试string是否含有匹配结果