【问题标题】:Javascript Regex to match emoticon symbols in my arrayJavascript Regex 匹配我数组中的表情符号
【发布时间】:2016-06-11 03:53:40
【问题描述】:

我有这个数组,其中包含每个 emo 路径的 emo 符号和关联的图像文件。

Working demo of my code on JSFIDDLE

但使用此代码,仅 :) 此 emo 会返回正确的 smile.png 图像,但 emo 的其余部分不起作用。

如何编写与每个符号匹配的正确正则表达式并为每个 emo 选择合适的文件?

 //Replace Emo with images
  function replaceEmoticons(text) {
  var emoticons = {
    ':)' : 'smile.png',
    ': )' : 'smile.png',
    ':D' : 'laugh.png',
    ':->' : 'laugh.png',
    ':d' : 'laugh-sm.png',
    ':-)': 'smile-simple.png',
    ':p': 'tounge.png',
    ':P': 'tounge-lg.png',
    ': P': 'tng1.png',
    '>-)': 'evil.png',
    ':(': 'sad.png',
    ':-(': 'sadd.png',
    ':-<': 'sadd.png',
    ':-O': 'surprise.png',
    ':O': 'sur2.png',
    ':o': 'sur3.png',
    ':-o': 'sur3.png',
    ':-*': 'kiss.png',
    ':*': 'kiss.png',
    ':-@': 'angry.png',
    ':@': 'angry.png',
    ':$': 'con2.png',
    ':-$': 'con1.png',
    'O.o': 'con2.png',
    'o.O': 'con2.png',
    ':/': 'weird.png',
    ':x': 'x.png',
    ':X': 'x.png',
    ':!': 's.png',
    '(:I': 'egg.png',
    '^.^': 'kit.png',
    '^_^': 'kit.png',
    ';)': 'wink.png',
    ';-)': 'wink.png',
    ":')": 'hc.png',
    ":'(": 'cry.png',
    "|-O": 'yawn.png',
    "-_-": 'poke.png',
    ":|": 'p1.png',
    "$_$": 'he.png'

  }, url = "images/emo/";
  // a simple regex to match the characters used in the emoticons
  return text.replace(/[:\-)D]+/g, function (match) {

    return typeof emoticons[match] != 'undefined' ?
           '<img class="emo" src="'+url+emoticons[match]+'"/>' :
           match;
  });
}


replaceEmoticons("Hi this is a test string :) with all :P emos working :D");

【问题讨论】:

标签: javascript jquery regex symbols emoticons


【解决方案1】:

这个正则表达式:

 [:\-)D]+

与您列表中的许多表情符号不匹配。 :\-)D 以外的任何字符都会阻止它被识别。

如果您有一个要匹配的字符串列表,您可以通过转义每个字符串并将它们与| 连接在一起,轻松构建一个正则表达式来匹配其中的任何一个(仅此而已)。像这样的:

// given a string, return the source for a regular expression that will 
// match only that exact string, by escaping any regex metacharacters
// contained within.
RegExp.escape = function(text) {
  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

// build a regex that will match all the emoticons. Written out, it looks
// like this:   /:\)|: \)|:D|:->|:d|:-\)|:p|:P|..../g
var emoticonRegex = 
  new RegExp(Object.keys(emoticons).map(RegExp.escape).join('|'), 'g');

然后用它代替你的文字正则表达式:

return text.replace(emoticonRegex, function (match) { ...

【讨论】:

  • 但是 OP 如何编写正则表达式? (我相信这是 OP 提出的问题)。
  • 我正谈到那部分,@cale_b。 :)
  • 嗯,我已经能够在我的字符串中写下所有的 emos 符号,这似乎有效。谁能解释一下正则表达式的工作原理?它需要里面每个角色的组合吗?这对我有用,我只是不明白它是如何工作的 text.replace(/[:;\-)DPp(oO|>
  • 谢谢,但我只是在我的正则表达式中使用每个字符的单个出现,而没有转义或加入任何带有管道符号的东西,这似乎也有效。我的方法还有其他不好的含义吗?它会将简单的短信视为 emo 吗?或者使用我的方式很好..这种方式对我有用 return text.replace(/[:^;\-)DPp(oO|>
  • 您的解决方案匹配所有表情符号,但它也会匹配列表中出现的那些字符的组合 - 例如,任意数量的冒号、连字符,或连续句点:::---... 全部匹配。哎呀,即使是句子中的单个括号也会匹配。并且当它找到一个在表中没有条目的匹配项时,它会将其替换为带有src="undefined"&lt;img&gt; 标记。
猜你喜欢
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
相关资源
最近更新 更多