0

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.

Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37

1 Answers1

1

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
anubhava
  • 761,203
  • 64
  • 569
  • 643