美文网首页
RegExp对象

RegExp对象

作者: 麓语 | 来源:发表于2018-08-07 14:15 被阅读0次

    正则定义

    var pattern = /s$/
    // 表示匹配所有以 s 结尾的字符串 
    

    也可以使用构造函数RegExp()定义

    var pattern = new RegExp('s$')
    

    RegExp实例属性

    属性 意义
    global 布尔值,表示是否设置了 g 标志。
    ignoreCase 布尔值,表示是否设置了 i 标志。
    lastIndex 整数,表示开始搜索下一个匹配的字符位置,从 0 算起。
    multiline 布尔值,表示是否设置了 m 标志。
    source 正则表达式的字符串表示,按照字面量形式而非传入构造函数中的字符串模式返回。
    var pattern1 = /\[bc\]at/i;
    console.log(pattern1.global)          // false
    console.log(pattern1.ignoreCase)      // true
    console.log(pattern1.lastIndex)       // 0
    console.log(pattern1.multiline)       // false
    console.log(pattern1.source)          // "/\[bc\]at/i" 
    
    var pattern2 = new RegExp("\\[bc\\]at");
    console.log(pattern2.global)          // false
    console.log(pattern2.ignoreCase)      // false
    console.log(pattern2.lastIndex)       // 0
    console.log(pattern2.multiline)       // false
    console.log(pattern2.source)          // "/\[bc\]at/i" 
    

    RegExp实例方法

    exec()方法

    RegExp对象的主要方法是exec(),该方法时专门为捕获组而设计。
    exec()接受一个参数:应用模式的字符串
    返回值:包含第一个匹配项信息的数组(在没有匹配项的情况下返回null)

    返回的数组虽然是Array的实例,但是包含两个额外属性: indexinput
    index表示匹配项在字符串中的位置, input表示应用正则表达式的字符串。

    var str = "mom and dad and baby";
    var pattern = /mom( and dad( and baby)?)?/gi
    var matches = pattern.exec(str);
    console.log(matches.index);        // 0
    console.log(matches.input);        // "mom and dad and baby"
    console.log(matches[0]);           // "mom and dad and baby"
    console.log(matches[1]);           // " and dad and baby"
    console.log(matches[2]);           // " and baby"
    

    模式中包含两个捕获组,最内部匹配and baby而包含的它的捕获组and dad或者and dad and baby

    对于 exec() 方法而言,即使在模式中设置了全局标志( g ),它每次也只会返回一个匹配项。在不设置全局标志的情况下,在同一个字符串上多次调用 exec() 将始终返回第一个匹配项的信息。而在设置全局标志的情况下,每次调用 exec() 则都会在字符串中继续查找新匹配项

    var text = "cat, bat, sat, fat";
    var pattern1 = /.at/;
    var matches = pattern1.exec(text);
    alert(matches.index);                   //0
    alert(matches[0]);                      //cat
    alert(pattern1.lastIndex);              //0
    
    matches = pattern1.exec(text);
    alert(matches.index);                   //0
    alert(matches[0]);                      //cat
    alert(pattern1.lastIndex);              //0
    
    var pattern2 = /.at/g;
    var matches = pattern2.exec(text);
    alert(matches.index);                   //0
    alert(matches[0]);                      //cat
    alert(pattern2.lastIndex);              //3
    
    matches = pattern2.exec(text);
    alert(matches.index);                   //5
    alert(matches[0]);                      //bat
    alert(pattern2.lastIndex);              //8
    

    这个例子中的第一个模式 pattern1不是全局模式,因此每次调用 exec() 返回的都是第一个匹配项("cat" )。而第二个模式 pattern2是全局模式,因此每次调用 exec()都会返回字符串中的下一个匹配项,直至搜索到字符串末尾为止。此外,还应该注意模式的 lastIndex属性的变化情况。在全局匹配模式下, lastIndex的值在每次调用 exec() 后都会增加,而在非全局模式下则始终保持不变。

    test()方法

    接受一个字符串参数。在模式与该参数匹配的情况下返回true;否则,返回 false。在只想知道目标字符串与某个模式是否匹配,但不需要知道其文本内容的情况下,使用这个方法非常方便。因此, test()方法经常被用在 if语句中

    var text = "000-00-0000";
    var pattern = /\d{3}-\d{2}-\d{4}/;
    if (pattern.test(text)){
      console.log("The pattern was matched.");
    }
    

    RegExp实例继承的 toLocaleString()toString()方法都会返回正则表达式的字面量,与创建正则表达式的方式无关。(正则表达式的 valueOf() 方法返回正则表达式本身。)例如:

    var pattern = new RegExp("\\[bc\\]at", "gi");
    console.log(pattern.toString());             // /\[bc\]at/gi
    console.log(pattern.toLocaleString());       // /\[bc\]at/gi
    

    相关文章

      网友评论

          本文标题:RegExp对象

          本文链接:https://www.haomeiwen.com/subject/dtbovftx.html