DIV CSS 佈局教程網

String.match() 與 RegExp.exec() 的差別完整說明
編輯:關於JavaScript     

這兩個方法外觀相似,都會回傳比對結果,但行為在 global 旗標打開與否時差異很大。搞不清楚兩者差別,很容易寫出只抓到第一筆就當成全部的程式碼。

共同点

1. 兩個方法比對成功時回傳一個陣列,失敗則回傳 null。

2. 當 RegExp 的 global 屬性為 false 時,這兩個方法的回傳陣列完全一樣。

此時陣列的第 0 個元素是整個 pattern 的第一個匹配字串,後續元素則是各個子匹配字串。此外,陣列還有 index 與 input 兩個額外屬性:index 是匹配字串的起始位置,input 是整個輸入字串。此時 RegExp 的 lastIndex 屬性一直是 0。

情況一:global 為 false

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

兩者輸出完全一致,這也是許多人以為它們等價的原因。

情況二:global 為 true

這時回傳的陣列就出现了明顯差異:

match 方法回傳的陣列只包含所有匹配到的完整字串,沒有子匹配字串,也沒有 index 與 input 等額外屬性。此時 lastIndex 屬性無效。

exec 方法回傳的陣列格式與 global 為 false 時相同(含子匹配與額外屬性),只是此時 RegExp 的 lastIndex 屬性變得有效:比對會從 lastIndex 所指示的位置開始,且方法執行後會把 lastIndex 設為本次匹配結束後的下一個字元位置。因此迴圈呼叫 exec 就能依序走訪整個字串,直到最後回傳 null,並把 lastIndex 歸零。

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

可以清楚看到:match 回傳的是兩個完整匹配字串,但沒有位置資訊;exec 則是每次回傳一筆完整結果,並透過 lastIndex 逐步推進,最後一次回傳 null 並歸零。

綜合結論

1. 沒有 g 旗標時,match 與 exec 效果相同;有 g 旗標時,exec 能提供最完整的匹配結果(含子群組與位置)。

2. 順帶一提 RegExp.test() 方法,它是 exec 的簡化版:有匹配結果就回傳 true,沒有就回傳 false,執行過程與 exec 一樣,相當於 (p.exec(s) != null)。

3. RegExp 的 lastIndex 屬性只在使用 g 旗標、且透過 exec 或 test 方法時有效,其他地方一律無效。

實務上的兩個提醒

共用正則物件會有狀態污染問題。由於 exec 會改動 lastIndex,如果把同一個含 g 旗標的正則物件重複用於多次比對,第二次的起點會沿用上次的結果。安全做法是每次比對前重設 regex.lastIndex = 0,或乾脆每次都建立新的正則實例。

需要「全部匹配 + 子群組」時,match 不夠用。此時應改用 String.matchAll()(會回傳 iterator,每筆都含完整群組與 index),或直接用 exec 迴圈。matchAll 要求正則必須帶 g 旗標,且不會改動原正則的 lastIndex,比共用 exec 安全得多。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved