No repeats please, test vs search for regular expression question

First time asking a question here. I’ve just completed the No Repeats Please challenge, but spent a lot of time trying to figure out why I couldn’t use the .test method to complete my algorithm, but .search worked.

Here’s my working solution with the .search method.

function permAlone(str) {
  let doubleRegEx = /(\w)\1+/g;
  function generate(string, permutation = '', permutations = []){
    if (!string){
    for (let i = 0; i < string.length; i++){
      generate(string.slice(0, i) + string.slice(i + 1), permutation + string[i], permutations);
    return permutations;
 return generate(str).filter(x => == -1).length;


At first, I thought .test made more sense, but filter kept passing some elements that should have matched the regular expression.

 return generate(str).filter(x => x..filter(x => !doubleRegEx.test(x)).length;

Can anyone help me?

You could use .test(), but you need to remove the g flag from the regex. Regex objects keep a lastIndex property (which tells where from the string it should start matching) that updates when you perform a .test and that regex has the g flag. When lastIndex is not 0, the regex might miss some parts of the string that it could otherwise match.

1 Like

I see! I’ll go and read up a bit more. Thanks so much!