gpt4 book ai didi

javascript - 即使是相同的输入,正则表达式在不同代码中的作用也不相同

转载 作者:数据小太阳 更新时间:2023-10-29 05:17:34 26 4
gpt4 key购买 nike

我正在创建一个代码,该代码的一部分使用了一个正则表达式:

var ex = "122", checker = /(\d)\1{1,}/g,
c = pattern.test(+ex);
if(c) console.log(`works.`);

我的目标是检查是否有重复的数字。

当我在较大的代码段中使用上面的代码片段时,问题就出现了。我在上面的代码片段中有相同的输入,但在下面的代码中,正则表达式不起作用

代码如下:

function almostIncreasingSequence(sequence) {
var clone = [].concat(sequence),
l = clone.length,
pattern = /(\d)\1{2,}/ig;
if (pattern.test(clone)) {
return false;
}

var sorted = sequence.splice(0).sort(function(a, b) {
return a - b;
}),
element, finder, checker = /(\d)\1{1,}/g;

for (var i = 0; i < l; ++i) {
element = clone[i];
finder = sorted.indexOf(element);
clone.splice(i, 1);
sorted.splice(finder, 1);
if (clone.join("") === sorted.join("") && !checker.test(+clone.join(""))) {
/*
In the conditional above, i use the regex
*/
console.log(`
Same input: ${+clone.join("")}

PD: This message, it should not be shown, since the conditional should not have been true,
`);
return true;
} else {
clone.splice(i, 0, element);
sorted.splice(finder, 0, element);
}
}
return false;
}
console.log(almostIncreasingSequence([1, 2, 1, 2]))

所以在第一个片段代码中,正则表达式返回 true,但在第二个片段代码中,或者正则表达式错误或者否定不起作用,因为我正在否定正则表达式的结果,因为正则表达式在第二段代码与第一段代码具有相同的输入,因此 true 的否定为 false。

最佳答案

问题是您在 Regexp 中使用了 /g - 当使用它并且多次执行 regex 时,它总是会从上次停止的地方开始。

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

This property is set only if the regular expression instance used the "g" flag to indicate a global search. The following rules apply:

If lastIndex is greater than the length of the string, test() and exec() fail, then lastIndex is set to 0.

If lastIndex is equal to the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at lastIndex.

If lastIndex is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and lastIndex is reset to 0.

Otherwise, lastIndex is set to the next position following the most recent match.

在执行 checker.test 之前,您可以通过记录 checker.lastIndex 来查看正在发生的情况,并且您将看到当它失败时,lastIndex 不为 0。

function almostIncreasingSequence(sequence) {
var clone = [].concat(sequence),
l = clone.length,
pattern = /(\d)\1{2,}/ig;
if (pattern.test(clone)) {
return false;
}

var sorted = sequence.splice(0).sort(function(a, b) {
return a - b;
}),
element, finder, checker = /(\d)\1{1,}/g;

for (var i = 0; i < l; ++i) {
element = clone[i];
finder = sorted.indexOf(element);
clone.splice(i, 1);
sorted.splice(finder, 1);
console.log(checker.lastIndex)
if (clone.join("") === sorted.join("") && !checker.test(+clone.join(""))) {
/*
checker.test(+clone.join("")) <-- HERE IS THE REGEX
*/
console.log( `The number is ${+clone.join("")}
and the regex return: ${checker.test(+clone.join(""))},
so, if the regex return true, the negation operator, isnt work ? because im negating the returned value from the regex !checker.test
`); // FALSE ? Why... ??
return true;
} else {
clone.splice(i, 0, element);
sorted.splice(finder, 0, element);
}
}
return false;
}
console.log(almostIncreasingSequence([1, 2, 1, 2]))

或者,使用您的第一个“工作”代码:

var ex = "122", checker = /(\d)\1{1,}/g,
c = checker.test(+ex);
if(c) console.log(`works.`);
c = checker.test(+ex);
if(c) console.log(`works.`);

而且它只会打印一次works,因为您检查它不止一次并且它在Regexp 上有一个/g

关于javascript - 即使是相同的输入,正则表达式在不同代码中的作用也不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47683748/

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