基于功能顺序的不同功能输出

时间:2019-06-14 19:23:36

标签: javascript regex do-while

我正在练习正则表达式。我在StackOverflow上找到了一个很好的答案: How do I retrieve all matches for a regular expression in JavaScript?

我从检查的答案中复制了代码,并将其粘贴到编辑器中,然后使用它编写了自己的函数。所以这是我的JS文件:

const regex = /[a-zA-Z]/g;
const _string = "++a+b+c++";

//The following is the loop from the StackOverflow user

var m;
do {
  m = regex.exec(_string);
  if (m) {
    // Logging the results
    console.log(m, "hello");
  }
} while (m);

console.log("Separating top and bottom so it's easier to read");
// Now here is the function I wrote using that code

const match = str => {
  let _matches;
  do {
    _matches = regex.exec(_string);
    if (_matches) {
      // Logging the results
      console.log(_matches);
    }
  } while (_matches);
}

match(_string);

这是我的问题:当我运行此代码(这是一个Repl.it)时,来自第一个函数的结果(因此,在这种情况下,来自stackoverflow用户的循环)不包括来自RegExp.prototype.exec() 方法。这是我的控制台输出:

 node v10.15.2 linux/amd64

 [ 'b', index: 4, input: '++a+b+c++', groups: undefined ] 'hello'
 [ 'c', index: 6, input: '++a+b+c++', groups: undefined ] 'hello'
 Separating top and bottom so it's easier to read
 [ 'a', index: 2, input: '++a+b+c++', groups: undefined ]
 [ 'b', index: 4, input: '++a+b+c++', groups: undefined ]
 [ 'c', index: 6, input: '++a+b+c++', groups: undefined ]

如果我切换循环和函数的顺序,该函数将不会返回第一个匹配项,而循环将返回所有三个匹配项。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

如果您只想从字符串中获取字母,则不需要任何循环。
你可以做

"++a+b+c+A+".match(/[a-z]/gi)

答案 1 :(得分:1)

我猜想我们可能在这里处理字符串,我们希望获取存在++a+b+c++的数字索引,该表达式可能会起作用:

index:\s*([0-9]+)\s*,.+'(\+\+a\+b\+c\+\+)'

Demo 1

const regex = /index:\s*([0-9]+)\s*,.+'(\+\+a\+b\+c\+\+)'/gm;
const str = `node v10.15.2 linux/amd64

 [ 'b', index: 4, input: '++a+b+c++', groups: undefined ] 'hello'
 [ 'c', index: 6, input: '++a+b+c++', groups: undefined ] 'hello'
 Separating top and bottom so it's easier to read
 [ 'a', index: 2, input: '++a+b+c++', groups: undefined ]
 [ 'b', index: 4, input: '++a+b+c++', groups: undefined ]
 [ 'c', index: 6, input: '++a+b+c++', groups: undefined ]`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

或者,如果我们只希望仅获取索引,

index:\s*([0-9]+)\s*,

const regex = /index:\s*([0-9]+)\s*,/gm;
const str = `node v10.15.2 linux/amd64

 [ 'b', index: 4, input: '++a+b+c++', groups: undefined ] 'hello'
 [ 'c', index: 6, input: '++a+b+c++', groups: undefined ] 'hello'
 Separating top and bottom so it's easier to read
 [ 'a', index: 2, input: '++a+b+c++', groups: undefined ]
 [ 'b', index: 4, input: '++a+b+c++', groups: undefined ]
 [ 'c', index: 6, input: '++a+b+c++', groups: undefined ]`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Demo 2