I am creating code, part of this code uses a regular expression that:
var ex = "122", checker = /(\d)\1{1,}/g,
c = pattern.test(+ex);
if(c) console.log(`works.`);
Run codeHide resultMy goal is to check if there is a duplicate number.
The problem occurs when I use the code snippet above in a larger section of code. I have the same input in the above snippet, but in the code below regex is not work
Here is the code:
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(""))) {
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]))
Run codeHide resultSo, in the first code snippet, regex returns true, but in the second code of the snippet or in the regular expression it is bad or the negation does not work, because I deny the result of the regular expression, because the regular expression in the second code of the snippet has the same input as the first, therefore, negating true is false.