简体   繁体   English

正则表达式呢?

[英]Regular expression for this?

How can I make a RegEx that matches something like these? 如何制作与此类匹配的RegEx?

"Hi hello world" and "Hi world"

I tried 我试过了

/^Hi (hello)? world$/i

But it doesn't work. 但这是行不通的。

/^Hi( hello)? world$/i

The space before hello (or the one after) also needs to be optional, otherwise it matches either: hello之前的空格(或之后的空格)也必须是可选的,否则它可以匹配:

  • hi__world (the underscores are "spaces") hi__world (下划线是“空格”)
  • hi hello world

应该这样做:

 /^Hi( | hello )world$/i

试试/^Hi (hello )?world$/i ,您的代码仅匹配“ Hi hello world”和“ Hi world”

Hi (\hello |)world

The pipe character works as "or." 竖线字符用作“或”。 So, the expression inside the parens matches either " hello" or nothing. 因此,parens中的表达式匹配“ hello”或不匹配。 You can test your Javascript regulare expressions here: http://rejex.heroku.com/ 您可以在此处测试Javascript正则表达式: http : //rejex.heroku.com/

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

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