• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

《javascript权威指南》正则式match和exec方法解析

时间:2015年04月02日 15:24:37    来源:魔法猪系统重装大师官网    人气:13077

最近看了《javascript权威指南》里面的正则部分,match和exec方法有一些相同点和不同点,在这里写一下加深一下印象。

1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null。

2. 当RegExp的global属性为false时,这两个方法的返回数组是一样的。

  数组的第0个元素是整个pattern的第一个匹配字符串,接下来的元素是pattern第一个匹配中的子匹配字符串。

  此外,数组还有index和input两个额外属性,index是匹配字符串的起始位置,input是整个输入字符串。

  此时,RegExp的lastIndex属性一直是0。

JavaScript权威指南 第五版
JavaScript权威指南 第五版 中文版高清扫描版 评分:
5.1
类别: 电子教程    大小:66.2M    语言: 中文
查看详细信息 >>

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

综上:

在没有g标识符时,match和exec方法效果是一样的;有g标识符时,exec方法可以提供最完整的匹配结果。

这里顺便提一下RegExp.test()方法,它是exec方法的简化版,有匹配结果就返回true,没有匹配结果就返回false,执行过程与exec是一样的。相当于 (p.exec(s) != null)。

RegExp的lastIndex属性在有g标识符,且在exec和test方法中是有效的,其他地方是无效的。

《,javascript权威指南,》,正则,式,match,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部