1

因为做一道题(http://www.codewars.com/kata/insert-dashes/solutions/javascript),题目如下:

Write a function insertDash(num) that will insert dashes ('-') between
each two odd numbers in num. For example: if num is 454793 the output
should be 4547-9-3. Don't count zero as an odd number.

其中一个解答,引起了我对正则的研究兴趣,解答如下:

function insertDash(num) {
   return String(num).replace(/([13579])(?=[13579])/g, '$1-');
}

我对正则表达式中的 正向肯定预查(?=pattern)一直不带明白,所以趁这个机会研究一下。

下面是我的测试代码:

var str = "13579";

var regArr = [
  /([13579])([13579])/g,    // capturing groups
  /([13579])(?:[13579])/g,  // non-capturing groups
  /([13579])(?=[13579])/g   // Assertions 
  ];


regArr.forEach(function(reg){
  console.log("regexp:", reg);
  while((q=reg.exec(str)) != null){
    console.log(q, "lastIndex", reg.lastIndex);
  }
  console.log("result:", str.replace(reg, "$1-"));
});

测试代码的输出:

regexp: /([13579])([13579])/g
["13", "1", "3", index: 0, input: "13579"] "lastIndex" 2
["57", "5", "7", index: 2, input: "13579"] "lastIndex" 4
result: 1-5-9
regexp: /([13579])(?:[13579])/g
["13", "1", index: 0, input: "13579"] "lastIndex" 2
["57", "5", index: 2, input: "13579"] "lastIndex" 4
result: 1-5-9
regexp: /([13579])(?=[13579])/g
["1", "1", index: 0, input: "13579"] "lastIndex" 1
["3", "3", index: 1, input: "13579"] "lastIndex" 2
["5", "5", index: 2, input: "13579"] "lastIndex" 3
["7", "7", index: 3, input: "13579"] "lastIndex" 4
result: 1-3-5-7-9

regexObj.exec(str) 返回结果是一个数组,其中第一个成员是所匹配的字符串,接下来是捕获的分组,它有一个index 属性,表明是从哪个地方开始匹配的。与此同时,reg 对象的 lastIndex 更新为下一次开始匹配的索引值。

  • /([13579])([13579])/g 每次匹配两个字符,下次开始匹配位置 +2, 每次产生两个捕获分组;

  • /([13579])(?:[13579])/g, 每次也匹配两个字符,下次开始匹配位置 +2, 由于使用了非捕获匹配,所以每次产生一个捕获分组;

  • /([13579])(?=[13579])/g, (?=[13579])只帮助定位匹配的位置,并不属于匹配的字符,所以每次匹配一个字符,所以下一次开始匹配位置 +1 ;

参考

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec


杨军军
1.3k 声望26 粉丝