NomadTraveler
NomadTraveler

Reputation: 1084

Regex match whole word including whitespace and fullstop

I am trying to build a regex that matches a word within <p> and <div> tags and replace the word with some other text. This word could be at the start of a tag or between other words or at the end of a sentence (trailed by a fullstop or a comma or a semicolon). My regex works, but not completely. Also the tags could have css classes as attributes.

My regex : [^<>\n]*\b(Cat|Dog|Fish)\b[^<>\n]*

So, if the text is something like this:

(1) <p> Cat test dfdsf</p>
(2) <p> Cat.</p>
(3) <p>Cat.</p>
(4) <p class="test">Cat</p>
(5) <div>Cat</div>
(6) <p>Catfgdggh</p>
(7) <li>Cat</li>

It should match all above but (6) and (7). Also only "Cat" should match and not the other words within the tag.

Any help would be much appreciated. Also, can you please give explanation. Thanks :)

Upvotes: 1

Views: 1980

Answers (1)

vks
vks

Reputation: 67968

\b(Cat|Dog|Fish)\b

Use \b or word boundary.

\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

Upvotes: 4

Related Questions