Brian Lai Brian Lai - 10 months ago 71
Javascript Question

javascript regular expression /e*/

Why the javascript regular expression


cannot match any part of the string "hello"?

I have already tried the following

"hello".match(/e*/) //return [""]

can match

"hello".match(/e+/) //return ["e"]

Am I overlooking any simple stuff????

Answer Source

It matched, or you would be getting null as a result.

The match is zero characters right at the beginning of the string. Your assertion is "find a place in the string where there is zero or more of e": Right at the start of the hello, there is zero or more of e, so we don't need to search further. match expectedly returns [""] (the zero characters matched).

On the other hand, /e+/ wants one or more e characters; this is not satisfied at the start of the string, but it is satisfied at the very next position, and you get ["e"].