javascript 中String.match()與RegExp.exec()的區(qū)別說(shuō)明
2. 當(dāng)RegExp的global屬性為false時(shí),這兩個(gè)方法的返回?cái)?shù)組是一樣的。
數(shù)組的第0個(gè)元素是整個(gè)pattern的第一個(gè)匹配字符串,接下來(lái)的元素是pattern第一個(gè)匹配中的子匹配字符串。
此外,數(shù)組還有index和input兩個(gè)額外屬性,index是匹配字符串的起始位置,input是整個(gè)輸入字符串。
此時(shí),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);
顯示結(jié)果為(firefox控制臺(tái)):
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. 當(dāng)RegExp的global屬性為true時(shí),返回的數(shù)組是不同的。
match方法返回的數(shù)組包含著所有匹配字符串,沒(méi)有子匹配字符串和額外屬性。此時(shí),lastIndex屬性無(wú)效。
exec方法返回的數(shù)組格式與global為false時(shí)一樣,只是此時(shí)RegExp的lastIndex屬性有效,匹配是從lastIndex所指示的字符開(kāi)始的,并且方法執(zhí)行后會(huì)將lastIndex置為本次匹配字符串的下一個(gè)字符處,所以循環(huán)執(zhí)行exec方法時(shí)會(huì)依次匹配整個(gè)字符串,直到字符串最后返回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);
結(jié)果:
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.在沒(méi)有g(shù)標(biāo)識(shí)符時(shí),match和exec方法效果是一樣的;有g(shù)標(biāo)識(shí)符時(shí),exec方法可以提供最完整的匹配結(jié)果。
2.這里順便提一下RegExp.test()方法,它是exec方法的簡(jiǎn)化版,有匹配結(jié)果就返回true,沒(méi)有匹配結(jié)果就返回false,執(zhí)行過(guò)程與exec是一樣的。相當(dāng)于 (p.exec(s) != null)。
3.RegExp的lastIndex屬性在有g(shù)標(biāo)識(shí)符,且在exec和test方法中是有效的,其他地方是無(wú)效的。
相關(guān)文章
jQuery NProgress.js加載進(jìn)度插件的簡(jiǎn)單使用方法
NProgress是基于jquery的,且版本要 >1.8 。這篇文章主要介紹了NProgress.js加載進(jìn)度插件的簡(jiǎn)單使用方法,需要的朋友可以參考下2018-01-01JavaScript實(shí)現(xiàn)的簡(jiǎn)單冪函數(shù)實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的簡(jiǎn)單冪函數(shù),實(shí)例分析了javascript實(shí)現(xiàn)冪運(yùn)算的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04JavaScript判斷數(shù)字是否為質(zhì)數(shù)的方法匯總
這篇文章主要介紹了JavaScript判斷數(shù)字是否為質(zhì)數(shù)的方法匯總的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Javascript如何實(shí)現(xiàn)雙指控制圖片功能
這篇文章主要介紹了Javascript如何實(shí)現(xiàn)雙指控制圖片功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02javascript ES6 Template String模板字符串使用方法
這篇文章主要介紹了javascript ES6 模板字符串(Template String)是增強(qiáng)版的字符串,用反引號(hào)(`)標(biāo)識(shí),它可以當(dāng)作普通字符串使用,也可以用來(lái)定義多行字符串,或者在字符串中嵌入變量,需要的朋友可以參考下2023-06-06JS遍歷ul下的li點(diǎn)擊彈出li的索引的實(shí)現(xiàn)方法
這篇文章主要介紹了JS遍歷ul下的li點(diǎn)擊彈出li的索引的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Knockout結(jié)合Bootstrap創(chuàng)建動(dòng)態(tài)UI實(shí)現(xiàn)產(chǎn)品列表管理
這篇文章主要為大家詳細(xì)介紹了Knockout結(jié)合Bootstrap創(chuàng)建動(dòng)態(tài)UI實(shí)現(xiàn)產(chǎn)品列表管理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09