離弦之Ray

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            55 Posts :: 0 Stories :: 33 Comments :: 0 Trackbacks

          Chapter 7  正則表達式

          1:定義RegExp對象:

                 1 var reCat = new RegExp(“cat”,”gi”); 

                 2 var reCar /Cat/gi

                 gglobal的縮寫,

                 iinsensitive的縮寫(表示大小寫不敏感)

           

          2 使用RegExp對象:

                 RegExp對象的方法:test() exec()

                String  對象的方法:match(), search(), replace(), split()

                

                 ――――――――――――――――――――――――――――――――――――

                 test 方法: 

              var sToMatch = "cat";

              var reCat = /cat/;

          alert(reCat.test(sToMatch));  //output true;

           

          ――――――――――――――――――――――――――――――――――――

                 exec方法: 

                 返回一個字符串數(shù)組, 第一個條目是第一個匹配, 其它的都是反向引用

              var sToMatch =    "http://msdn.microsoft.com:80/scripting/default.htm";

              var reS = /(\w+):\/\/([^:]+)(:\d*)?([^# ]*)/;

              var arrMatches=reS.exec(sToMatch);

              alert(arrMatches.length);

              for(i=1;i<arrMatches.length;i++)

                  alert(arrMatches[i]);
                

                 exec()方法返回一個數(shù)組, 數(shù)組中的第一個題目是第一個匹配, 其它的都是反向引在上面的例子中:
                        a[0]
          是匹配到的字符串,即:http://msdn.microsoft.com:80/scripting/default.htm
                        a[1]
          包含 "http"
                        a[2]
          包含 "msdn.microsoft.com"
                        a[3]
          包含 ":80"
                        a[4]
          包含 "/scripting/default.htm"
                 (
          也可以用RegExp.$1RegExp.$2RegExp.$3RegExp.$4取值)

           

          ――――――――――――――――――――――――――――――――――――

          match方法

          返回一個包含在字符串中所有匹配的數(shù)組

              var sToMatch = "a bat, a cat, afat baT, a faT cat";

              var reAt = /at/gi;

              var arrMatches  = sToMatch.match(reAt);

              for(i = 1; i < arrMatches.length; i++)

                  alert(arrMatches[i]);

           

          ――――――――――――――――――――――――――――――――――――

          search方法

          返回在字符串中出現(xiàn)的第一個匹配的位置

              var sToMatch = "a bat, a cat, afat baT, a faT cat";

              var reAt = /at/gi;

          alert(sToMatch.serach(reAt)); //output 3

           

          ――――――――――――――――――――――――――――――――――――

          replace方法

          用第二個參數(shù)來替換第一個參數(shù),返回替換后的字符串

                 var sToChange = "The sky is red the sky is red";

              var reRed = /red/;

              alert(sToChange.replace(reRed,"blue"));

              Result: The sky is blue, the sky is red.

           

              如果要替換“red”的所有出現(xiàn), 必須指明/red/g

              var reRed = /red/g;

              alert(sToChange.replace(reRed,"blue"));

              Result: The sky is blue, the sky is blue.

           

              Replace最強大的用法是接受一個函數(shù)作為第二個參數(shù)。這個函數(shù)可以接受一個參數(shù)(即  匹配正則表達式的文本),并返回替換文本。

           

          ――――――――――――――――――――――――――――――――――――

          split方法

          將字符串分割成一系列子串,并通過數(shù)組返回。

              var sColor = "red,blue,black,white,yellow,green";

              var reComma = /\,/;

              var arrColors = sColor.split(reComma);

              for(i=1;i<arrColors.length;i++)

             alert(arrColors[i]);

           

           

          3 正則表達式的組成部分

          l         元字符

          l         字符類

          l         量詞

           

          31 元字符:

          ( [ { \ ^ $ | ) ? * + . , 使用上述的元字符時,需要轉(zhuǎn)義

          例如:

              var reQMark = /\?/;

              var reQMark = new RegExp("\\?");兩個反斜杠是因為: ”\”在字符串中本身需要轉(zhuǎn)義

           

           

          32 字符類

          簡單類:[abc]  匹配abc

          負向類:[^abc] 匹配除了abc的所有字符

          范圍類:[a-z] 匹配az

          組合類:由上述幾種字符類組合而成, 內(nèi)部的類之間不能有空格 [a-zA-Z0-9]

          預定義類:

          代碼

          等同于

          匹配

          .

          [^\n\r]

          除了換行和回車之外的任意

          \d

          [0-9]

          數(shù)字

          \D

          [^0-9]

          非數(shù)字

          \s

          [ \t\n\x0B\f\r]

          空白字符

          \S

          [^ \t\n\x0B\f\r]

          非空白字符

          \w

          [a-zA-Z0-9_]

          單詞字符(字母數(shù)字下劃線)

          \W

          [^a-zA-Z0-9_]

          非單詞字符

           

           

          33 量詞

          331簡單量詞:  

          代碼

          描述

          ?

          出現(xiàn)0次或1

          *

          出現(xiàn)0次或多次

          +

          出現(xiàn)1次或多次

          {n}

          一定出現(xiàn)n

          {n,}

          至少出現(xiàn)n

          {n,m}

          出現(xiàn)n次到m

           

          正則表達式

          描述

          ba?d

          bd, bad

          ba+d

          bad, baad, baaad,…

          ba*d

          bd, bad, baad, baaad,…

          ba{1}d

          bad

          ba{1,}d

          bad, baad, baaad,…

          ba{1,2}d

          bad, baad

           

          332 貪婪的,惰性的和支配性的量詞

          貪婪量詞:

                 先看整個字符串是否匹配;如果沒有,去掉最后一個字符;重復這個過程,一直到發(fā)現(xiàn)       匹配或者字符串不剩余任何字符。

          惰性量詞:

                 先看字符串中的第一個字符是否匹配, 如果不, 則讀入下一個字符,重復一直到發(fā)現(xiàn)       匹配或者整個字符串都已經(jīng)檢查過了。
          支配性量詞:

                 只嘗試匹配整個字符串。(IE不支持, Mozilla視為貪婪量詞處理)

           

          貪婪

          惰性

          支配

          ?

          ??

          ?+

          *

          *?

          *+

          +

          +?

          ++

          {n}

          {n}?

          {n}+

          {n,m}

          {n,m}?

          {n,m}+

          {n,}

          {n,}?

          {n,}+

           

              var sToMatch = "abbbaabbbaaabbb1234";

              var re1 = /.*bbb/g;

              var re2 = /.*?bbb/g;

              //var re3 = /.*+bbb/g;

             

              var arrRes1 = sToMatch.match(re1);

              var arrRes2 = sToMatch.match(re2);

              //var arrRes3 = sToMatch.match(re3);

             

              alert(arrRes1);   //output "abbbaabbbaaabbb"

              alert(arrRes2); //output "abbb","aabbb","aaabbb"

           

           

          4 分組

                 分組即用一系列的括號包圍一系列字符, 字符類以及量詞

                 示例: 輕松實現(xiàn)Stringtrim方法:

              String.prototype.trim = function(){

                  var reExtraSpace = /^\s*(.*?)\s+$/;

                  return this.replace(reExtraSpace,"$1");  

              }  

             

              var sTest = "    hello, guy  ";

              alert(sTest.trim());

           

           

           

          5 反向引用

                 如果正則表達式中含有分組, 分組中的值將被保存。反向引用就是存儲在分組中的特殊值

                 例如表達式(a?(b?(c?)))將產(chǎn)生編號從13的三個反向引用:
                 (1): (A?(B?(C?)))

                 (2): (B?(C?))

                 (3): (C?)

                 獲取反向引用:RegExp.$1, RegExp.$2

          在正則表達式中包含反向引用: /(dog)\1/  /dogdog/

                 反向引用在replace方法中:$1 $2    

           

           

          6 候選

                 var reRedOrBlack = /(red|black)/

           

           

          7 非捕獲性分組

                 不創(chuàng)建反向引用:(?:pattern)

                 var renumbers = /#(?:\d+)/;

           

           

          8 前瞻

                 正向前瞻檢查的是接下來出現(xiàn)的是不是某個特定的字符

                 負向前瞻檢查的是接下來的不應該出現(xiàn)的字符

                 JavaScript不支持負向前瞻

                 示例:

                 var sToMatch1 = "bedroom";

              var reBed = /bed(?=room)/;

              alert(reBed.test(sToMatch1)); //output true

           

           

          9 邊界

           

          邊界

          描述

          ^

          行開頭

          $

          行結(jié)尾

          \b

          單詞的邊界

          \B

          非單詞的邊界

           

          10 多行模式

                 var reMultiLine = /pattern/m;

           

              var sToMatch = "First second" +

                 " third forth" +

                 " fifth sixth";

              var reLastWord1 = /(\w+)$/g;

              var reLastWord2 = /(\w+)&/gm;

              var reBeginWord1 = /^(\w+)/g;

              var reBeginWord2 = /^(\w+)/gm

           

              alert(sToMatch.match(reLastWord1));

              alert(sToMatch.match(reLastWord2));

              alert(sToMatch.match(reBeginWord1));

              alert(sToMatch.match(reBeginWord2));

          日期的正則表達式:

          ^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$

           

          加了時間驗證的

          ^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$


          posted on 2008-01-03 14:13 離弦之ray的技術(shù)天空 閱讀(243) 評論(0)  編輯  收藏 所屬分類: JavaScript

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 苗栗市| 红河县| 金山区| 永泰县| 济宁市| 荔波县| 广灵县| 九台市| 满洲里市| 沭阳县| 齐齐哈尔市| 肥东县| 绥中县| 中方县| 客服| 广昌县| 渝中区| 乐都县| 汉寿县| 深州市| 黄浦区| 青岛市| 建始县| 汝阳县| 忻城县| 中方县| 城口县| 青岛市| 进贤县| 明水县| 乌拉特后旗| 容城县| 肃宁县| 萨迦县| 萨嘎县| 平阴县| 石柱| 唐河县| 建宁县| 五家渠市| 施秉县|