无为清净楼资源网 Design By www.qnjia.com
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方法中是有效的,其他地方是无效的。
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方法中是有效的,其他地方是无效的。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月30日
2024年09月30日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】