I try to replace some specials characters (emojis) based on a replacement map.
I wrote this piece of code but it still not work..
var char_map = {
'■': 'e',
'♥': 'a',
'♦': 'm',
};
$.fn.map_replace = function() {
$(this).html(function(i, content) {
$.each(char_map, function(key, value) {
content = content.replace(key, value);
});
return content;
});
};
$(".comment").map_replace();
Thanks for helping 🙂
One way to do this would be to use escape codes instead of literal Unicode characters, and replace them using regular expressions:
Updated jsFiddle
The reason your initial approach didn’t work is that by default String.replace() only replaces the first occurrence of the search expression. By using a regex and specifying the “g” (for “global”) flag, you can tell it to replace all matches.