I have following JavaScript code.
var emoticons = {
':)': '<span class="emoticon emoticon_smile"></span>',
':-)': '<span class="emoticon emoticon_smile"></span>',
':(': '<span class="emoticon emoticon_sad"></span>',
':d': '<span class="emoticon emoticon_bigSmile"></span>',
':D': '<span class="emoticon emoticon_bigSmile"></span>'
}
and now to replace the emotion with span in the given text I am using following functions
function Emotions (text) {
if (text == null || text == undefined || text == "") return;
var pattern = /[:\-)(D/pPy@'*]+/gi;
return text.replace(pattern, function (match) {
return typeof emoticons[match] != 'undefined' ?
emoticons[match] :
match;
});
}
Now the above code is working fine.If I pass the text in the function as below
Emotions("Hey this is a test :( :(");
see the space between 2 emotions character. But if I remove the space between both the emotion then it does not work.Like below
Emotions("Hey this is a test :(:(");
There is something wrong with my regular expression but I am not been able to figure it out.
I have following JavaScript code.
var emoticons = {
':)': '<span class="emoticon emoticon_smile"></span>',
':-)': '<span class="emoticon emoticon_smile"></span>',
':(': '<span class="emoticon emoticon_sad"></span>',
':d': '<span class="emoticon emoticon_bigSmile"></span>',
':D': '<span class="emoticon emoticon_bigSmile"></span>'
}
and now to replace the emotion with span in the given text I am using following functions
function Emotions (text) {
if (text == null || text == undefined || text == "") return;
var pattern = /[:\-)(D/pPy@'*]+/gi;
return text.replace(pattern, function (match) {
return typeof emoticons[match] != 'undefined' ?
emoticons[match] :
match;
});
}
Now the above code is working fine.If I pass the text in the function as below
Emotions("Hey this is a test :( :(");
see the space between 2 emotions character. But if I remove the space between both the emotion then it does not work.Like below
Emotions("Hey this is a test :(:(");
There is something wrong with my regular expression but I am not been able to figure it out.
Share Improve this question asked Jun 14, 2014 at 14:58 Vimal PatelVimal Patel 3,0952 gold badges27 silver badges38 bronze badges 2-
2
BTW, you can use:
return emoticons[match] || match;
instead. – Gaurang Tandon Commented Jun 14, 2014 at 15:03 - jsfiddle/AbQ23 – Gaurang Tandon Commented Jun 14, 2014 at 15:13
5 Answers
Reset to default 6/:-?[()pPdD]/gi
characters in [] brackets are posibilites and remaining by order
The expression cannot tell that ":(:(" should be treated as two "blocks" since ":(:(" perfectly satisfies your regex pattern
.
If you, for example, know that all your emojis start with a colon (:) you could use the following expression to check for emoji "blocks"
:[\-)(D/pPy@'*]+
(the colon is now in front of the character-class)
When you delete the space, your whole emotion sequence :(:(
is match by your pattern /[:\-)(D/pPy@'*]+/
(as a single sequence). As there is no span associated with :(:(
, nothing is returned.
You should find a way to delimit your character sequence (e.g. by adding a space in front of your pattern like this / [:\-)(D/pPy@'*]+/
, but don't forget in that case to add a space in front of your span).
try to change your regular expression to
var pattern = /:[\-)(D/pPy@'*]+/gi;
USe This Expresion search and your code will work
var pattern = /:[(-)dD]+/gi;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744195990a4562658.html
评论列表(0条)