LocustHorde
LocustHorde

Reputation: 6399

Regex match whole word not working

apologies for the rookie question.

I have a small journal where I am letting users comment on stuff that I post and am converting certain characters to smileys.

so :) becomes an image <img src='\smiley\smile.png' /> and :d becomes <img src='\smiley\big-smile.png' /> and so on and so forth.

Now, recently, one of my friends posted an educational link which had a :d in the url and my smiley regex jumped at the link and broke it into pieces, with a big smile image.

You get the Idea.

So I changed my regex from :d to \b:d\b and expected it to match whole word, if :d is all by itself. Guess what? the regex picks up NOTHING now.

here is a sample demonstration of what I am talking about

How do I get the regex to match only :d on its own? thanks.

Upvotes: 0

Views: 491

Answers (2)

H&#229;vard
H&#229;vard

Reputation: 10080

That's because \b matches word boundaries. It works when you put it behind the :d, because the d is considered a word. : is not considered a word character, and thus is not a word boundary. Fix it with a lookbehind for whitespace or an anchor:

(?<=^|\s):d\b

Edit: as Bob Vale pointed out, this also applies if you are matching a smiley like :/, / does not trigger a word boundary. You have to do the same thing, but with a lookahead:

(?<=^|\s):d(?=$|\s)

Upvotes: 3

Bob Vale
Bob Vale

Reputation: 18474

You will need to use look behind and look ahead matches on beginning / end of string and whitespace as the characters you are trying to match won't necessarily trigger the usual word boundary rules.

use(?<=^|\s):d(?=$|\s) this pattern should work for all of your matches eg (?<=^|\s):\)(?=$|\s)

Upvotes: 1

Related Questions