user4913383 user4913383 - 1 year ago 68
Javascript Question

Why does "ab".match(/ab/) returns ["ab","ab"]?

Why does following:


returns this:

["ab", "ab"]

Though "ab" appears once in the string "ab" , why has "ab" appeared twice in the array?

Answer Source

The first element contains the entire matching string, while the next elements contain the group matches. Consider:

> "somestring44".match(/[a-z]([a-z]*)/)
["somestring", "omestring"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download