This function's job is to replace several text smilies, e.g. :D
, :)
, :love
with the appropriate smiley image.
In my opinion my code has several issues, yet the main problem is that a lot of quite similar regexes are created dynamically: one for each different smilie replaced.
As you might expect, replacing smilies in a text is a job done kindly often, so this is indeed code that is executed a lot of time, therefore I think optimizing that makes sense.
Well, why didn't I just call text.replace()
?
I don't want the smilies to be replaced if they are enclosed by backticks: `
var replaceTextSmilies = function() {
var smiliesShortcut = {};
smiliesShortcut[':)'] = 'smile.png';
/* adding much more */
var smiliesWordy = {};
smiliesWordy[':angry'] = 'angry.png';
/* adding much more */
function escapeRegExp(str) {
return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
return function(text) {
Object.keys(smiliesWordy).forEach(function(entry) {
var image = '<img src="img/smilies/' + smiliesWordy[entry] + '"/>';
if(text.indexOf(entry) !== -1) {
var regex = new RegExp('('+ escapeRegExp(entry) +')(?=(?:(?:[^`]*`){2})*[^`]*$)', 'g');
text = text.replace(regex, image);
}
});
Object.keys(smiliesShortcut).forEach(function(entry) {
var image = '<img src="img/smilies/' + smiliesShortcut[entry] + '"/>';
if(text.indexOf(entry) !== -1) {
var regex = new RegExp('('+ escapeRegExp(entry) +')(?=(?:(?:[^`]*`){2})*[^`]*$)', 'g');
text = text.replace(regex, image);
}
});
return text;
};
}();