jquery - Using regular Expression to replace emotions character in javascript - Stack Overflow

I have following JavaScript code.var emoticons = {':)': '<span class="emoticon e

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
Add a ment  | 

5 Answers 5

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信