Regex matches a whole word not working

apologies for the rookie.

I have a small journal where I allow users to comment on the things I post and convert some characters to emoticons.

therefore :) becomes the image <img src='\smiley\smile.png' /> and : d becomes <img src='\smiley\big-smile.png' /> etc. etc.

Recently, one of my friends posted a training link in which he had :d , and my regular expression of emoticons jumped over the link and broke it into pieces with a large image of a smile.

You get the Idea.

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

here is an example of a demonstration of what I'm talking about

How to get regex only for :d ? thanks.

0
source share
2 answers

You will need to use the look and see the matches at the beginning / end of the line and the space, since the characters you are trying to match will not necessarily trigger the usual word boundary rules.

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

+1
source

This is because \b matches word boundaries. It works when you put it behind :d , because d is considered a word. : not considered a symbol of a word and therefore is not a word boundary. Correct it with lookbehind for space or anchor:

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

Edit: as Bob Val pointed out, this also applies if you match a smiley like :/ , / does not call a word boundary. You should do the same, but considering:

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

Source: https://habr.com/ru/post/893363/


All Articles