I have JavaScript code.
var emoticons = { ':)': '<span class="emoticon emoticon_smile"></span>', ':-)': '<span class="emoticon emoticon_smile"></span>', ':(': '<span class="emoticon emoticon_sad"></span>', ':d': '<span class="emoticon emoticon_bigSmile"></span>', ':D': '<span class="emoticon emoticon_bigSmile"></span>' }
and now, to replace emotion with a range in this text, I use the following functions
function Emotions (text) { if (text == null || text == undefined || text == "") return; var pattern = /[:\-)(D/ pPy@ '*]+/gi; return text.replace(pattern, function (match) { return typeof emoticons[match] != 'undefined' ? emoticons[match] : match; }); }
Now the above code works fine. If I pass the text to functions as below
Emotions("Hey this is a test :( :(");
see the space between the two symbols of emotions. But if I remove the space between the emotions, then it will not work. Below
Emotions("Hey this is a test :(:(");
Something is wrong in my regex, but I could not figure it out.
source share