#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Is this a bug or something I don't understand...


    Hi,
    I enter something under in Console of Chrome DevTools:

    /(a)?/.exec("caandy")
    ["", undefined]

    /(a)?/.exec("acaandy")
    ["a", "a"]

    why /(a)?/.exec("caandy") don't get ["a", "a"] ?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    Because the "a" is optional. Chrome checked the first letter to do due diligence: in the first example it didn't match while in the second example it did. It didn't have to do any more work beyond that. Heck, it didn't even have to do that at all, but it might be programmed so that it begins checking the string before/while it's "compiling" the regex.
    Last edited by requinix; December 23rd, 2013 at 05:05 AM.

IMN logo majestic logo threadwatch logo seochat tools logo