简体   繁体   中英

Regex that matches word end AND isn't a “#”

My regex currently detects a "ence" suffix:

/ence\b/

However, I do not want it to match on ence# . How can I say "match on ence\\b , but that end-of-word cannot be "#"?

You may use a negative lookahead:

/ence\b(?!#)/
       ^^^^^

See the Rubular demo

Details :

  • ence - a literal substring
  • \\b - a word boundary (since it is preceded with a word char, e , the next char must be a non-word char or end of string)
  • (?!#) - the negative lookahead that fails the match if there is a # immediately to the right of the current location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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