0

I have problem with testing string with regex. After iterations "aab", "aab", "aba".. here comes the problem when testing string "baa" first time is ok, result is false because regex test is setup to check is there repeating letter inside string, but when testing again "baa" result is now true. Why is this happening?

Here is the code:

//Here are function for swaping letters         
String.prototype.swapLetters=function(index) {
        var temp = this.split("");
        var n = temp[index];
        temp[index]=temp[index+1]; temp[index+1]=n;
        var str1 = temp.join("");
        return str1;
    }



 function permAlone(str) {
//the function for calculating number of total combinations
        function returnFactorial(num){
            if(num===0){
                return 1;
            } else {
                return returnFactorial(num-1)*num;
            }
        }
        var combs = returnFactorial(str.length);
        var c = 0;
        var permutations = 0;
        var reg = new RegExp(/(.)\1+/g);
        for (var i = 0; i < combs; i++) {
            if(c>=str.length-1){
                c = 0;
            } 
            str = str.swapLetters(c);
            if(!reg.test(str)){
                permutations++;
                console.log(str);
            }

            c++;
        }
    }
permAlone('aab');
Enis Jasarovic
  • 125
  • 1
  • 8

1 Answers1

0

Firstly, the condition you have if(!reg.test(str)){} should not have a !(not) in it if you intend to identify a regex match. I am not sure if that is what you wanted, though.

Secondly, I removed the 'global' match flag 'g' so that the regex is basically "reset" to start matching from the beginning of the text before each execution. This is because a single RegExp object when executed multiple times, starts to match the text each time from the last match index. This can give you a detailed explanation for this. Why RegExp with global flag in Javascript give wrong results?.

Try this.

//Here are function for swaping letters         
String.prototype.swapLetters=function(index) {
        var temp = this.split("");
        var n = temp[index];
        temp[index]=temp[index+1]; temp[index+1]=n;
        var str1 = temp.join("");
        return str1;
    }

function permAlone(str) {
//the function for calculating number of total combinations
        function returnFactorial(num){
            if(num===0){
                return 1;
            } else {
                return returnFactorial(num-1)*num;
            }
        }
  
        var combs = returnFactorial(str.length);
        var c = 0;
        var permutations = 0;
        var reg = new RegExp(/(\w)\1+/);
        for (var i = 0; i < combs; i++) {
            if(c>=str.length-1){
                c = 0;
            } 
            str = str.swapLetters(c);
            console.log("case : " + str);
            if(reg.test(str)){
                permutations++;
                console.log(str + " has repeated letters");
            }

            c++;
        }
    }
permAlone('aab');
Community
  • 1
  • 1
Piyush
  • 1,162
  • 9
  • 17