javascript 中String.match()與RegExp.exec()的區別說明

來源:互聯網
上載者:User

1. 這兩個方法,如果匹配成功,返回一個數組,匹配失敗,返回null。
2. 當RegExp的global屬性為false時,這兩個方法的返回數組是一樣的。

  數組的第0個元素是整個pattern的第一個匹配字串,接下來的元素是pattern第一個匹配中的子匹配字串。
  此外,數組還有index和input兩個額外屬性,index是匹配字串的起始位置,input是整個輸入字串。
  此時,RegExp的lastIndex屬性一直是0。
demo: 複製代碼 代碼如下:var s = 'this is a string';
var p = /\b\w*(i)s\b/;
var rm = s.match(p);
var re = p.exec(s);
console.log('match_array: ' + JSON.stringify(rm));
console.log('match_array_index: ' + rm.index);
console.log('match_array_input: ' + rm.input);
console.log('----------------------------');
console.log('exec_array: ' + JSON.stringify(re));
console.log('exec_array_index: ' + re.index);
console.log('exec_array_input: ' + re.input);

顯示結果為(firefox控制台): 複製代碼 代碼如下:match_array: ["this","i"]
match_array_index: 0
match_array_input: this is a string
----------------------------
exec_array: ["this","i"]
exec_array_index: 0
exec_array_input: this is a string

3. 當RegExp的global屬性為true時,返回的數組是不同的。
  match方法返回的數組包含著所有匹配字串,沒有子匹配字串和額外屬性。此時,lastIndex屬性無效。
  exec方法返回的數組格式與global為false時一樣,只是此時RegExp的lastIndex屬性有效,匹配是從lastIndex所指示的字元開始的,並且方法執行後會將lastIndex置為本次匹配字串的下一個字元處,所以迴圈執行exec方法時會依次匹配整個字串,直到字串最後返回null,並將lastIndex置0。
demo: 複製代碼 代碼如下:var s = 'this is a string';
var p = /\b\w*(i)s\b/g;
var rm = s.match(p);
var re;
console.log('match_array: ' + JSON.stringify(rm));
console.log('match_array_index: ' + rm.index);
console.log('match_array_input: ' + rm.input);
while(re = p.exec(s)){
console.log('----------------------------');
console.log('exec_array: ' + JSON.stringify(re));
console.log('exec_array_index: ' + re.index);
console.log('exec_array_input: ' + re.input);
console.log('regexp_lastIndex: ' + p.lastIndex);
}
console.log('----------------------------');
console.log('exec_array: ' + re);
console.log('regexp_lastIndex: ' + p.lastIndex);

結果: 複製代碼 代碼如下:match_array: ["this","is"]
match_array_index: undefined
match_array_input: undefined
----------------------------
exec_array: ["this","i"]
exec_array_index: 0
exec_array_input: this is a string
regexp_lastIndex: 4
----------------------------
exec_array: ["is","i"]
exec_array_index: 5
exec_array_input: this is a string
regexp_lastIndex: 7
----------------------------
exec_array: null
regexp_lastIndex: 0

綜上:

1.在沒有g標識符時,match和exec方法效果是一樣的;有g標識符時,exec方法可以提供最完整的匹配結果。
2.這裡順便提一下RegExp.test()方法,它是exec方法的簡化版,有匹配結果就返回true,沒有匹配結果就返回false,執行過程與exec是一樣的。相當於 (p.exec(s) != null)。
3.RegExp的lastIndex屬性在有g標識符,且在exec和test方法中是有效,其他地方是無效的。

相關文章

聯繫我們

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