Brian Lai Brian Lai - 17 days ago 5
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

/e*/


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

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"].