简体   繁体   中英

Regexp - match specific word allowing special characters

I wanna make a regular expression to find specific word allowing special characters( \\W ). For example, if the word is replace then:

  • replace matched.
  • replace( , $replace , replace , replace matched because of allowing special characters.
  • areplace , replacea not matched because of a .

It seems like I have to use (?=) operator, but I have no idea about how to use it.

You may use this regex with word breaks and allowing any non-word, non-whitespace on either side:

[^\w\s]*\breplace\b[^\w\s]*

RegEx Demo

RegEx Details:

  • [^\\w\\s]0 : Match optional 0+ non-word and non-whitespace character
  • \\b : Word boundary
  • replace : match text replace
  • \\b : Word boundary
  • [^\\w\\s]? : Match optional 0+ non-word and non-whitespace character

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