JS之RegExp对象(二)

版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/zkn_CS_DN_2013/article/details/24243159


RegExp对象的经常用法有test、exec和compile,本节介绍这些方法的功能和用法。

最后,对RegExp对象的属性和方法进行一个综合举例。

2.1  test方法

语法格式为test(str)。该方法检查一个字符串中是否存在创建RegExp对象实例时所指定的表达式模式,假设存在就返回True,否则返回False。假设找到匹配项,则会更新RegExp对象中的有关静态属性。以反映匹配情况。

 

2.2  exec方法

语法格式为exec(str)。该方法使用创建RegExp对象实例时所指定的表达式模式对一个字符串进行搜索。并返回一个包括搜索结果的数组。

     假设为正則表達式设置了全局标志(g),能够通过多次调用exec和test方法在字符串中进行连续搜索,每次都是从RegExp对象的lastIndex属性值指定的位置開始搜索字符串。

     假设没有设置全局标志(g),则exec和test方法忽略RegExp对象的lastIndex属性值,从字符串的起始位置開始搜索。

假设exec方法没有找到匹配,返回值为null;假设找到匹配,则返回一个数组,并更新RegExp对象中有关静态属性以反映匹配情况。返回数组中的元素0包括了完整的匹配结果。而元素1~n依次是表达式模式中定义的各个子匹配的结果。

exec方法返回的数组有3个属性,各自是input、index和lastIndex。

     input属性是整个被搜索的字符串。

     index属性是指匹配在整个被搜索字符串中的位置。

     lastIndex属性是指匹配的子字符串的最后一个字符的下一个字符位置。

代码2.1是对该方法的应用举例。

代码2.1  exec()方法应用:2.1.htm

<html>

<head>

<title>exec()方法应用</title>

<scriptlanguage = "JavaScript">

    varmyString="aaa 111 bbb 222 ccc 1111 222ddd";

    varregex =/111/;    //创建正則表達式对象

    vararray=regex.exec(myString);

    if(array){

         var str="找到了匹配子串!"+" 返回数组的值为:"+array+" 数组元素个数:"

                         +array.length+" 被搜索的字符串为:"+array.input

                         +" 匹配子串的開始位置为:"+array.index

                         +" 匹配子串后面第一个字符的位置为:"+regex.lastIndex;

                 alert(str);

         }

         else{

                 alert("未找到匹配子串。!");

         }

</script>

<body></body>

</html>

2.3  compile方法

语法格式为compile("pattern"[,"flags"])。该方法能够更换RegExp对象实例所使用的表达式模式,并将新的表达式模式编译为内部格式,从而使以后的匹配过程运行更快。假设要在循环中反复使用某个表达式,对其进行编译将使运行加速。可是,假设在程序中使用了不论什么其它表达式模式后,再使用原来编译过的表达式模式,则这样的编译毫无  益处。

2.4   综合举例

代码2.2是对RegExp对象的综合举例,认真分析代码及其运行结果。能够更好地理解RegExp对象。

代码2.2  RegExp对象的使用:2.2.htm

<scriptlanguage="javascript">

      var strSrc = "xxa1b01c001yya2b02c002zz";

      var re = /a(d)b(d{2})c(d{3})/gi;

      var arr, count = 0;

      while ((arr = re.exec(strSrc)) != null)

      {

             displayResult();

      }

      function displayResult()

      {

             document.write("<p>这是用正則表達式/" + re.source + "/gi对字符串<br>""

                   + RegExp.input + ""进行第" + (++count) + "次搜索的结果:<br>");

             document.write("RegExp.index为" + RegExp.index + "<br>");

             document.write("RegExp.lastIndex为" + RegExp.lastIndex + "<br>");

             document.write("RegExp.lastMatch为" + RegExp.lastMatch + "<br>");

             document.write("RegExp.lastParen为" + RegExp.lastParen + "<br>");

             document.write("RegExp.leftContext为" + RegExp.leftContext + "<br>");

             document.write("RegExp.rightContext为" + RegExp.rightContext +"<br>");

             document.write("RegExp.$1为" + RegExp.$1 + "<br>");

             document.write("RegExp.$2为" + RegExp.$2 + "<br>");

             document.write("RegExp.$3为" + RegExp.$3 + "<br>");

             document.write("RegExp.$4为" + RegExp.$4 + "<br>");

             document.write("arr.index为" + arr.index + "<br>");

             document.write("arr.input为" + arr.input + "<br>");

             document.write("arr.lastIndex为" + arr.lastIndex + "<br>");

             document.write("返回数组的元素个数为" + arr.length + "<br>");

             document.write("返回数组的内容为["];

             for(var i=0; i<arr.length; i++)

             {

                 if(i < arr.length-1)

                     document.write(""" + arr[i] + "",");

                 else

                     document.write(""" + arr[i] + "")</p>");

             }

      }

</script>

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10653886.html