Reputation: 1943
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.
Upvotes: 0
Views: 748
Reputation: 785058
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 Details:
[^\w\s]0
: Match optional 0+ non-word and non-whitespace character\b
: Word boundaryreplace
: match text replace
\b
: Word boundary[^\w\s]?
: Match optional 0+ non-word and non-whitespace characterUpvotes: 1