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:):('));
use |
you could do something like:
I'm pretty sure you don't need the funny stuff you do at the beginning of each
for
loop to modify the regex. Get rid of that, and there's nothing stopping you from merging the two sets together.However, you should escape the special characters
(
and)
in your smileys when making the regexes. Also, I would suggest making the regex search case-insensitive so that<dog>
and<DOG>
are both matched.