gpt4 book ai didi

javascript - 基于函数顺序的不同函数输出

转载 作者:行者123 更新时间:2023-11-30 13:59:28 25 4
gpt4 key购买 nike

我正在练习正则表达式。我在 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 用户的循环)不包括从正则表达式.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 ]

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

任何帮助将不胜感激!

最佳答案

我猜我们可能在这里处理字符串,我们希望获得 ++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

关于javascript - 基于函数顺序的不同函数输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56604081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com