jsRegex

來源:互聯網
上載者:User

標籤:

1.exec

exec 尋找並返回當前的匹配結果,並以數組的形式返回。

 var str1="2003-11-01 "; var reg=new RegExp("\\d{4}-\\d{2}-\\d{2}", "gi"); alert(reg.exec(str1)); // --> "2003-11-01"

 

未匹配到,返回null

匹配到,總是返回1維數組。傳回值是一個對象,為當前匹配項,多個匹配時返回第一個匹配項。

注意匹配項 g 參數,若指定了 g,則下次調用 exec 時,會從上個匹配的 lastIndex 開始尋找。在反覆執行時,返回下一個匹配項。

var str = "1a1b1c";  var reg = new RegExp("1.", "g");  alert(reg.exec(str)[0]);  // --> ["1a"]alert(reg.exec(str)[0]);  // --> ["1b"]

 

此例子第一次返回 “1a”,第二次返回“1b”。

如果取消g 參數,兩次均返回“1a”

 

2.match

match 是 String 對象的一個方法。

 

var str = "1a1b1c";  var reg = new RegExp("1.", ""); alert(str.match(reg));   //  -->  ["1a"]str.match("/1./g")  // --> ["1a", "1b", "1c"]

 

注意與exec方法調用區別:match是string對象方法,exec是RegExp對象方法。

如果有g參數的話,返回的是所有匹配的數組。如上例中返回的是[1a,1b,1c]的數組。

 

jsRegex

聯繫我們

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