The regex does not work the same in other code, even being the same input

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 result

My 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(""))) {
            /*
            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]))
Run codeHide result

So, 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.

+6
2

, /g Regexp - , , , .

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

, "g" . :

lastIndex , test() exec(), lastIndex 0.

lastIndex , , , lastIndex.

lastIndex , , , lastIndex - reset to.

lastIndex .

, checker.lastIndex, checker.test, , , 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]))
Hide result

, "" :

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.`);
Hide result

works , , /g Regexp.

+6

: :

function almostIncreasingSequence (array) {
  return Array.from(array)
    .sort()
    .reduce(function(acc, cur, i, arr) {
      if (arr[i - 1] !== cur) {
        return acc.concat(cur)
      } else {
        return acc
      }
    }, [])
}

console.log(almostIncreasingSequence([1, 2, 1, 2])) // [1,2]
console.log(almostIncreasingSequence([1, 2, 2, 4, 3])) // [1,2,3,4]
Hide result
0
source

All Articles