javascript 中String.match()與RegExp.exec()的區(qū)別說明
更新時(shí)間:2013年01月10日 15:33:28 作者:
最近看了javascript權(quán)威指南 里面的正則部分,match和exec方法有一些相同點(diǎn)和不同點(diǎn),在這里寫一下加深一下印象
1. 這兩個(gè)方法,如果匹配成功,返回一個(gè)數(shù)組,匹配失敗,返回null。
2. 當(dāng)RegExp的global屬性為false時(shí),這兩個(gè)方法的返回?cái)?shù)組是一樣的。
數(shù)組的第0個(gè)元素是整個(gè)pattern的第一個(gè)匹配字符串,接下來的元素是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ù)組包含著所有匹配字符串,沒有子匹配字符串和額外屬性。此時(shí),lastIndex屬性無效。
exec方法返回的數(shù)組格式與global為false時(shí)一樣,只是此時(shí)RegExp的lastIndex屬性有效,匹配是從lastIndex所指示的字符開始的,并且方法執(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.在沒有g(shù)標(biāo)識(shí)符時(shí),match和exec方法效果是一樣的;有g(shù)標(biāo)識(shí)符時(shí),exec方法可以提供最完整的匹配結(jié)果。
2.這里順便提一下RegExp.test()方法,它是exec方法的簡(jiǎn)化版,有匹配結(jié)果就返回true,沒有匹配結(jié)果就返回false,執(zhí)行過程與exec是一樣的。相當(dāng)于 (p.exec(s) != null)。
3.RegExp的lastIndex屬性在有g(shù)標(biāo)識(shí)符,且在exec和test方法中是有效的,其他地方是無效的。
2. 當(dāng)RegExp的global屬性為false時(shí),這兩個(gè)方法的返回?cái)?shù)組是一樣的。
數(shù)組的第0個(gè)元素是整個(gè)pattern的第一個(gè)匹配字符串,接下來的元素是pattern第一個(gè)匹配中的子匹配字符串。
此外,數(shù)組還有index和input兩個(gè)額外屬性,index是匹配字符串的起始位置,input是整個(gè)輸入字符串。
此時(shí),RegExp的lastIndex屬性一直是0。
demo:
復(fù)制代碼 代碼如下:
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)):
復(fù)制代碼 代碼如下:
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ù)組包含著所有匹配字符串,沒有子匹配字符串和額外屬性。此時(shí),lastIndex屬性無效。
exec方法返回的數(shù)組格式與global為false時(shí)一樣,只是此時(shí)RegExp的lastIndex屬性有效,匹配是從lastIndex所指示的字符開始的,并且方法執(zhí)行后會(huì)將lastIndex置為本次匹配字符串的下一個(gè)字符處,所以循環(huán)執(zhí)行exec方法時(shí)會(huì)依次匹配整個(gè)字符串,直到字符串最后返回null,并將lastIndex置0。
demo:
復(fù)制代碼 代碼如下:
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é)果:
復(fù)制代碼 代碼如下:
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(shù)標(biāo)識(shí)符時(shí),match和exec方法效果是一樣的;有g(shù)標(biāo)識(shí)符時(shí),exec方法可以提供最完整的匹配結(jié)果。
2.這里順便提一下RegExp.test()方法,它是exec方法的簡(jiǎn)化版,有匹配結(jié)果就返回true,沒有匹配結(jié)果就返回false,執(zhí)行過程與exec是一樣的。相當(dāng)于 (p.exec(s) != null)。
3.RegExp的lastIndex屬性在有g(shù)標(biāo)識(shí)符,且在exec和test方法中是有效的,其他地方是無效的。
相關(guān)文章
Javascript自執(zhí)行匿名函數(shù)(function() { })()的原理淺析
匿名函數(shù)就是沒有函數(shù)名的函數(shù)。這篇文章主要介紹了Javascript自執(zhí)行匿名函數(shù)(function() { })()的原理淺析的相關(guān)資料,需要的朋友可以參考下2016-05-05
JavaScript實(shí)現(xiàn)可拖拽的拖動(dòng)層Div實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)可拖拽的拖動(dòng)層Div的方法,拖拽頁(yè)面中的div塊可實(shí)現(xiàn)div塊按照拖動(dòng)軌跡移動(dòng)的效果,涉及javascript鼠標(biāo)事件、頁(yè)面元素樣式結(jié)合事件函數(shù)動(dòng)態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2015-08-08
javascript動(dòng)畫效果打開/關(guān)閉層
用javascript實(shí)現(xiàn)打開層和關(guān)閉層的效果,原理不錯(cuò),學(xué)習(xí),記錄好2008-06-06
如何用JS WebSocket實(shí)現(xiàn)簡(jiǎn)單聊天
這篇文章主要介紹了如何用JS WebSocket實(shí)現(xiàn)簡(jiǎn)單聊天,對(duì)websocket感興趣的同學(xué),可以參考下2021-05-05
詳解如何在Canvas上實(shí)現(xiàn)坐標(biāo)定位
這篇文章我們將來詳細(xì)的給大家講解一下如何在 canvas 上實(shí)現(xiàn)坐標(biāo)的定位,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08
js操作時(shí)間(年-月-日 時(shí)-分-秒 星期幾)
js操作時(shí)間(年-月-日 時(shí)-分-秒 星期幾),需要的朋友可以參考下。2010-06-06
面向?qū)ο罄^承實(shí)例(a如何繼承b問題)(自寫)
經(jīng)常會(huì)看到a如何繼承b的問題;決定寫一下,其實(shí)繼承就是繼承父級(jí)的屬性和方法,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07

