benhowdle89
benhowdle89

Reputation: 37464

Regex: match a word that's on its own, ie. not adjoined by other letters

My test data:

<div class="row"></div> // should match
<div class="row line"></div> // should match
<div class="item row line"></div> // should match
<div class="rowBody"></div> // shouldn't match
var $itemRow = 1; // shouldn't match

I'm trying to replace all instances of the class row from a codebase.

My miserable attempts:

row[\W]{0}

and

row[\W]

Obviously neither have worked, what would I be looking for?

Upvotes: 1

Views: 561

Answers (2)

Kita
Kita

Reputation: 2634

How about using \b the word boundary?

/\brow\b/

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336198

That's what word boundary anchors are for:

\brow\b

Such an anchor matches the (zero-length) position between a character of the \w set and either a character of the \W set or a string boundary (start/end).

Upvotes: 3

Related Questions