0

我正在尝试修改一个名为 ReChat 的插件,以便它支持除默认表情之外的其他表情。

我需要制作这个正则表达式来替换整个单词我知道你需要添加\b元字符但我不能让它工作。

例如,如果有两个带有名称的表情符号,emotICON它将emotI始终显示emotI.

这是装载机:

ReChat.Playback.prototype._loadEmoticons = function() {
  var that = this;
  this._emoticons = [];
  ReChat.get('https://api.twitch.tv/kraken/chat/emoticons', {}, function(result) {
    $.each(result.emoticons, function(i, emoticon) {
      var image = emoticon.images[0];
      if (image.emoticon_set === 27) {
        that._emoticons.push({

          regex: new RegExp(emoticon.regex, 'g'),
          code: $('<span>').addClass('emoticon').css({ 'background-image': 'url(' + image.url + ')', 'height': image.height, 'width': image.width }).prop('outerHTML').replace(/&quot;/g, "'")
        });
      }
    });
  });
};

这是替代品:

ReChat.Playback.prototype._replaceEmoticons = function(text) {
    $.each(this._emoticons, function(i, emoticon) {

        text = text.replace(emoticon.regex, emoticon.code);

    });
    return text;
}

如果您需要完整的源代码,请访问https://github.com/pencil/rechat

4

1 回答 1

0

好吧,你需要使用\b两次。所以它可能是这样的:

/\bword\b/

这是因为\b特殊字符用于边界,它不匹配任何特定字符。

您也可以使用\s,它匹配空格:

/\sword\s/
于 2014-11-15T22:28:01.747 回答