简体   繁体   English

用\\ B实现RegExp与不匹配包含字母的图释的图释

[英]Implementing RegExp with \B for emoticons not matching for emoticons containing letters

I am developing a chat client for a game project and am in the process of implementing emoticons. 我正在为一个游戏项目开发一个聊天客户端,并且正在实施表情符号。 The basic rules of where emoticons should show up in the chat is that they do not appear when they are directly next to text. 表情符号在聊天中显示位置的基本规则是,表情符号直接位于文本旁边时不显示。

I created the Regular Expression: \\B(emoticontext)\\B. 我创建了正则表达式:\\ B(emoticontext)\\ B。

Unfortunately, I am having a problem where this works perfectly fine for every emoticons except the ones that contain letters. 不幸的是,我遇到一个问题,即除了包含字母的表情符号外,其他表情符号都可以很好地工作。 (eg :D, O_o, etc.) (例如:D,O_o等)

I am not sure how to remedy the situation. 我不确定如何纠正这种情况。

function parseEmoticons(text) {
    var pattern;
    emoticons.forEach(function (emoticon) {
        pattern = new RegExp("\\B" + emoticon.string + "\\B", 'g');
        text = text.replace(pattern, emoticon.img);
    });
    return text;
}

Here is a part of the emoticons array, for context. 对于上下文,这是表情符号数组的一部分。

  { 'string': ':\\)', 'img': '<img src="' + imgpath + 'emoticons/smile.png" class="emoticon"/>' },
    { 'string': ':O', 'img': '<img src="' + imgpath + 'emoticons/surprised.png" class="emoticon"/>' },
    { 'string': ':D', 'img': '<img src="' + imgpath + 'emoticons/happy.png" class="emoticon"/>' },

do not appear when they are directly next to text 当它们直接位于文本旁边时不显示

That sounds more like you want to check for surrounding whitespace, not \\B (a "non-word-boundary"). 听起来更像是您要检查周围的空格,而不是\\B (“非单词边界”)。

That is: 那是:

var pattern = new RegExp('(^|\\s)' + emoticon.string.replace(/\W/g, '\\$&') + '(?!\\S)', 'g');
text = text.replace(pattern, function (m0, m1) { return m1 + emoticon.img; });

Points of note: 注意事项:

  • (^|\\s) checks for (and captures) beginning of string or a whitespace character (^|\\s)检查(并捕获)字符串或空格字符的开头
  • .replace(/\\W/g, '\\\\$&') escapes all potential regex meta-characters in the emoticon (this means you'll probably have to change ':\\\\)' to ':)' in your emoticon list) .replace(/\\W/g, '\\\\$&')在图释中转义所有潜在的正则表达式元字符(这意味着您可能必须在图释中将':\\\\)'更改为':)'清单)
  • (?!\\S) ("not followed by a non-space character") makes sure the emoticon is either followed by whitespace or end-of-string (we can't use the same trick at the beginning because JavaScript doesn't support look-behind) (?!\\S) (“非后跟非空格字符”)可确保表情符后接空格或字符串结尾(我们一开始不能使用相同的技巧,因为JavaScript不会支持后退)
  • since we've potentially captured a space character at the beginning, we have to substitute it back in along with our HTML code 由于我们可能在一开始就捕获了空格字符,因此我们必须将其替换为HTML代码
  • we could do that with .replace(pattern, '$1' + emoticon.img) but that will cause problems if emoticon.img ends up containing one of the special $ patterns that .replace understands and interprets 我们可以使用.replace(pattern, '$1' + emoticon.img)来做到这一点,但是如果emoticon.img最终包含.replace理解和解释的特殊$模式之一,那将引起问题。
  • instead we go with a replacement function, which gets the whole matched string and the capture groups (and some other stuff) as arguments (but we only care about the first capture group) 相反,我们使用了替换函数,该函数将整个匹配的字符串和捕获组(以及其他一些东西)作为参数(但是我们只关心第一个捕获组)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM