Brian Lai Brian Lai - 1 year ago 86
Javascript Question

javascript regular expression /*e/

I think this may be a fairly simple and strict forward question but i cannot figure out the reason. Please kindly help.

Why the javascript regular expression


cannot match any part of the string "hello"?

I have already tried the following

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

while /+e/ 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"].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download