转载 正则用法

来源:互联网 发布:电信网络诈骗情形 编辑:程序博客网 时间:2024/06/08 01:27
<html>
    <head>
        <mce:script type="text/javascript"><!--
          window.onload = init;
          function init(){
            //testSearch();
            //testTest();
            testExecs();
          }
          //String method 找出字符串所有匹配的字符
          function testMatch(){
            var str = "a cat where a cat you are Cat";
            var regExp = /cat/gi;
            var array = str.match(regExp);
            for(var i=0; i<array.length; i++){
                alert(array[i]);
            }
          }
          //类似于index方法 ,String method
          function testSearch(){
            var str = "we are the world!";
            var regExp = /re/gi;
            alert(str.search(regExp));
          }
          //RegExp method 测试是否匹配模式
          function testTest(){
            var str = "a cat and a cat";
            var regExp = /cat/;
            alert(regExp.test(str));
          }
          //RegExp method 找出首次出现的字符串
          function testExecs(){
            var str = "this is a car";
            var regExp = /i/gi;
            var strArray = regExp.exec(str);
            alert(strArray.constructor == Array);
            alert(regExp.exec(str));
          }
        
// --></mce:script>
    </head>
    <body>
        
        
    </body>
</html>