JavaScript Regex(RegExp)

來源:互聯網
上載者:User

標籤:刪除   class   www.   res   Regex   strong   doc   als   eee   

什麼是RegExp

RegExp是一種模式用來描述要檢索的內容。

定義RegExp

1 var patt = new RegExp("模式");

RegExp對象的方法

RegExp對象有3個方法:test()、exec()、compile()

test()

檢索字串中指定的值。傳回值為true或者false

1 var patt = new RegExp("e");2 document.write(patt.test("I am a student");3 //傳回值4 true

 

exec()

檢索字串中指定的值。傳回值為找到的值,如果沒有發現匹配,返回null。

1 var patt = new RegExp("e");2 document.write(patt.exec("I am a student.);3 //傳回值4 e

可以設定第二個參數來設定檢索

g:全域檢索

i:執行對大小寫不敏感的匹配

m:執行多行匹配

1 var patt = new RegExp("e", "g");2 do {3     result = patt.exec("I am a student in the university");4     document.write(result);5 } while (result != null);6 //執行結果7 eeenull

compile()

用於改變RegExp。

即可以改變檢索模式,也可以添加或刪除第二個參數。

1 var patt = new RegExp("e");2 document.write(patt.test("I am a student");3 patt.compile("b");4 document.write(patt.test("I am a student");5 //輸出結果6 truefalse

 

 

 總結自:http://www.w3school.com.cn/js/js_obj_regexp.asp

JavaScript Regex(RegExp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.