简体   繁体   English

javascript正则表达式/ e * /结果为空匹配

[英]javascript regular expression /e*/ results in empty match

Why the javascript regular expression 为什么使用JavaScript正则表达式

/e*/ 

cannot match any part of the string "hello"? 不能匹配字符串“ hello”的任何部分?

I have already tried the following 我已经尝试了以下

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

while /e+/ can match /e+/可以匹配

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

Am I overlooking any simple stuff???? 我可以忽略任何简单的东西吗????

It matched, or you would be getting null as a result. 它匹配,否则结果将为null

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. 您的断言是“在e找到零个或多个e的字符串中找到一个位置”:在hello开头, e零个或多个e ,所以我们不需要进一步搜索。 match expectedly returns [""] (the zero characters matched). match预期返回[""] (匹配的零个字符)。

On the other hand, /e+/ wants one or more e characters; 另一方面, /e+/ 一个或多个e字符; this is not satisfied at the start of the string, but it is satisfied at the very next position, and you get ["e"] . 在字符串开头不满足此要求,但在下一个位置满足要求,您得到["e"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM