Exclusive Or in Regular Expression

后端 未结 13 1770
小鲜肉
小鲜肉 2020-12-05 10:06

Looking for a bit of regex help. I\'d like to design an expression that matches a string with \"foo\" OR \"bar\", but not both \"foo\" AND \"b

13条回答
  •  情书的邮戳
    2020-12-05 10:27

    I'd use something like this. It just checks for space around the words, but you could use the \b or \B to check for a border if you use \w. This would match " foo " or " bar ", so obviously you'd have to replace the whitespace as well, just in case. (Assuming you're replacing anything.)

    /\s((foo)|(bar))\s/
    

提交回复
热议问题