js中RegExp對象01

來源:互聯網
上載者:User

標籤:style   blog   color   使用   java   ar   for   div   sp   

一:定義方式

  ①字面量方式    var pattern = /pattern/attributes

  ②建構函式方式  var pattern = new RegExp("pattern","attributes");

  attributes參數:可以取值 g (global:是否全域)  ;  i (ignoreCase:是否忽略大小寫)  ;  m(multiline:是否多行)

  如果前邊的"pattern"是一個RegExp對象,則必須刪除該參數,否則會有TypeError異常拋出;

  《傳回值》返回一個新的RegExp對象;但是如果只是使用RegExp()方法而沒有使用new,並且pattern是一個RegExp對象的時候,就只是返回pattern而不會創造新對象;

  《拋出異常》

  SyntaxError - 如果 pattern 不是合法的Regex,或 attributes 含有 "g"、"i" 和 "m" 之外的字元,拋出該異常。

  TypeError - 如果 pattern 是 RegExp 對象,但沒有省略 attributes 參數,拋出該異常。

二:執行個體屬性

  這些都沒有什麼太大用處,暫時可以參考《JavaScript進階程式設計》(第3版)P105 下方的內容;以後有時間更新;

  lastIndex用的比較多;開始搜尋下一個匹配項的字元位置,從0開始;

三:執行個體方法

  compile();這個只有在W3c上面見到,具體什麼作用我到現在還不是很明白,望明白的大神賜教;

  exec();  用於檢測Regex在字串中的匹配,並返回一些結果;RegExpObject.exec(string)

    當Regex中沒有g標誌的時候,此方法返回結果與String對象中支援Regex的方法match()相同;返回一個數組;但是exec()方法返回的數組中,第一個元素

    是當前匹配內容,但是第二個是Regex中第一個子運算式(擷取的群組)的匹配內容(如果有的話),第三個是Regex中第二個子運算式(擷取的群組)的匹配內容(如果有的話),依次類推;

    但是當含有g的時候,String中的match()能夠一次性將所有符合的找到,但是exec()一次只找到一個;

    exec()方法返回除了數組和length屬性以外,還有index屬性和input屬性;index中存放的是當前匹配內容在字串中的位置,input是用於匹配的字串的內容;

    如果含有g,每執行一次exec(),RegExp的執行個體對象屬性lastIndex的值就會發生一次變化,值變成當前匹配項最後一個字元位置的下一個位置,下次搜尋匹配

    項的時候,就會從這個位置開始,而不會從頭開始,如果到最後沒有找到合適的,exec()方法就會返回null。 

 提示和注釋

    重要事項:如果在一個字串中完成了一次模式比對之後要開始檢索新的字串,就必須手動地把 lastIndex 屬性重設為 0。

    提示:請注意,無論 RegExpObject 是否是全域模式,exec() 都會把完整的細節添加到它返回的數組中。這就是 exec() 與 String.match() 的不同之處,後者在全域模式下返回的資訊要少得多。因此我    們可以這麼說,在迴圈中反覆地調用 exec() 方法是唯一一種獲得全域模式的完整模式比對資訊的方法。

  

<script>        var text = "mom and dad and baby mom and dad and baby";        var pattern = /(mom (and dad (and baby)?)?)/gi;        var matches;        while((matches = pattern.exec(text)) != null){            document.write("index:"+matches.index);            document.write("<br/>");            document.write("input:"+matches.input);            document.write("<br/>");            document.write("matches長度:"+matches.length);            document.write("<br/>");            document.write(matches[0]);            document.write("<br/>");            document.write(matches[1]);            document.write("<br/>");            document.write(matches[2]);            document.write("<br/>");            document.write(matches[3]);            document.write("<br/>");        }</script>

    下面是執行結果:

index:0input:mom and dad and baby mom and dad and babymatches長度:4mom and dad and babymom and dad and babyand dad and babyand babyindex:21input:mom and dad and baby mom and dad and babymatches長度:4mom and dad and babymom and dad and babyand dad and babyand baby

 

  test();方法用於檢測一個字串是否匹配某個模式.RegExpObject.test(string)

     當Regex的執行個體對象使用該方法的時候,執行個體屬性,lastIndex也會發生類似於exec()的變化。 下面是一個執行個體:  

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>RegExp 對象的內容03</title></head><body>    <script>        var re = null;        var i;        for(i=0;i<10;i++){            re = /cat/g;            var t1 = re.test("catastrophe");            document.write(t1);//true            document.write("<br/>");            document.write(re.lastIndex);//3            document.write("<br/>");            var t2 = re.test("catastrophe");            document.write(t2);//false            document.write("<br/>");            document.write(re.lastIndex);//0            document.write("<br/>");        }        document.write("<br/>");        for(i=0;i<10;i++){            re = new RegExp("cat","g");            var t3 = re.test("catastrophe");            var t4 = re.test("catastrophe");            document.write(t3);//true            document.write("<br/>");            document.write(t4);//false            document.write("<br/>");        }        document.write("<br/>");        var str = "catastrophe";        for(i=0;i<10;i++){            re = new RegExp("cat","g");            var t5 = str.match(re);            document.write(t5);//cat            document.write("<br/>");            document.write(re.lastIndex);//0            //如果是使用String對象支援的match(),則正則執行個體屬性lastIndex不會發生變化            document.write("<br/>");        }    </script></body></html>

四:RegExp建構函式屬性

  目前這些都不是很重要,暫時可以參考《JavaScript進階程式設計》(第3版)P105 下方的內容;以後有時間更新;

js中RegExp對象01

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.