3

I'm confused:

function is_valid(name) {
    var regexp_name = /^(\d|\w)*$/gi;
    return regexp_name.test(name);
}

// Console
console.log(is_valid("Test"));
=> true

console.log(is_valid("Test"));
=> false

console.log(is_valid("Test"));
=> true

console.log(is_valid("Test"));
=> false

What am I doing wrong?

2
  • I'm getting the same result as you in Firebug. Commented Jul 25, 2010 at 15:44
  • same here. surprisingly if you test it with console.log(is_valid("!#")); it turns out fine. and according to prolonged observations, the value returned alternates between true and false. Commented Jul 25, 2010 at 15:45

1 Answer 1

6

Remove the /g flag.

The RegExp object is somehow reused. When the /g flag is present, the regex engine will start from the previous matched location until the whole string is consumed.

 1st call:       Test
                 ^
 after 1st call: Test   (found "Test")
                     ^
 2nd call:       Test
                     ^
 after 2nd call  Test   (found nothing, reset)
                 ^

BTW, \w is equivalent to [0-9a-zA-Z_] in Javascript. Therefore, the \d| and the /i flag are redundant. And since you're not using the captured group, there's no need to keep the (…). This following is enough:

var regexp_name = /^\w*$/;
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, but why? How about an explanation?
I'm not understanding why it isn't consuming the entire string. The match is to the end of the string.
@George: $ is only an assertion. It won't consume anything. An empty string at the end is a possible match. Try using var re = /\w*/g; return re.exec(name);. You'll see the empty string is matched in the end.
It's alternating between true to false because the regex object is reused as @KennyTM said. The g flag makes the lastIndex property (where to begin next search) jump from 0 (true - always finds a match - "Test") to 4 (false - no matches from here onwards). Wrote some tests on this answer to confirm this behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.