I’m trying to implement word boundaries in my emoticons feature for a chat. But for some reason I can’t seem to get the word boundaries to work. I am new to regex.
So when I do:
var reg = /\b\Hi\b/gi;
var str = 'HiHiHi Hi HiHiHi Hi';
alert(str.replace(reg, ''));
This happens: Jsfiddle
It actually works fine, and does remove those 2 Hi’s that are standing alone.
But when I change the reg to an escaped smiley and then change the string:
var reg = /\b\:\)\b/gi;
var str = 'HiHi:) :) HiHiHi :)';
alert(str.replace(reg, ''));
This happens:
Jsfiddle
It just doesn’t work. The string stays the same. Is it that word boundaries can’t be used on symbols? If so, how does Facebook do it on their chats?
As
\bwill not work in this case, you could use:Which works like a space boundary.
You can add several smileys to it like so: