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?

Poru
  • 8,254
  • 22
  • 65
  • 89
  • I'm getting the same result as you in Firebug. – Steven Jul 25 '10 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. – mauris Jul 25 '10 at 15:45

1 Answers1

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*$/;
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 3
    Ok, but why? How about an explanation? – George Marian Jul 25 '10 at 15:49
  • I'm not understanding why it isn't consuming the entire string. The match is to the end of the string. – George Marian Jul 25 '10 at 15:56
  • @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. – kennytm Jul 25 '10 at 16:02
  • 1
    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](http://stackoverflow.com/questions/3152452/unexpected-javascript-behaviour-issue/3152837#3152837) to confirm this behavior. – Anurag Jul 25 '10 at 18:44