var data = ed.getContent();
this._emoticons = tinymce.util.JSON.parse(ed.getParam('moodleemoticon_emoticons'));
for (var emotxt in this._emoticons) {
- data = data.replace(emotxt, this._emoticons[emotxt]);
+ // escape the metacharacters so we can use it as regexp
+ search = emotxt.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
+ // convert to regexp
+ search = new RegExp(search, 'g');
+ // and replace all occurrences of it with the image
+ data = data.replace(search, this._emoticons[emotxt]);
}
ed.setContent(data);
});