I need to create a faunctionality, that will allow me to parse a text looking for a match for emoticon string in different format. It can be either a :)
type emoticon, or a <dog>
(<dog>
) type.
Currently my function can find both types but needs two different regexes for this task, and I'd like to make it in just one step. Any ideas ?
Live example at jsfiddle: http://jsfiddle.net/rmQSx/5/
and the code :
function replaceEmoticons(text) {
var emots1 = {
':)': 'smile.gif',
':(': 'sad.gif',
},
emots2 = {
'<dog>': 'dog.gif',
'<fly>': 'fly.gif'
},
regex1,regex2,p,re;
for(p in emots1){
regex1 = "["+p+"]{2}";
re = new RegExp(regex1, "g");
text = text.replace(re, function (match) {
return typeof emots1[match] != 'undefined' ?
'<img src="'+emots1[match]+'"/>' :
match;
});
}
for(p in emots2){
regex2 = "("+p+")(|$)";
re = new RegExp(regex2, "g");
text = text.replace(re, function(match) {
return typeof emots2[match] != 'undefined' ?
'<img src="'+emots2[match]+'"/>' :
match;
});
}
return text;
}
console.log(replaceEmoticons('this is <dog>b a<fly> simple test :) , ;) and more:):('));