正则表达式来查找和替换冒号内的表情符号名称 [英] Regex to find and replace emoji names within colons

查看:141
本文介绍了正则表达式来查找和替换冒号内的表情符号名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式(用于JavaScript的正则表达式引擎),我可以用它来查找和替换冒号内的表情符号名称的文本。就像在Slack或Discord中,当您键入:smiley-face:时,提交聊天时它将替换它。我只针对文本节点,所以我不必担心文本中的其他html。

I'm trying to write a regex (for JavaScript's regex engine) that I can use to do a find and replace in text for emoji names within colons. Like in Slack or Discord when you type :smiley-face: and it replaces it when you submit the chat. I'm targeting text nodes only so I don't need to worry about other html inside the text.

是否有可能编写一个可以匹配所有遵循规则? (用等宽空格突出显示的文本=正则表达式正匹配项)

Is it possible to write a regex that could match all of the following rules? (text highlighted with monospace blocks = regex positive matches)

:任何非空白:

:text1: sample2:

:@(1 @#$ @ SD: :s:

:nospace :: inbetween:,因为其中有2个冒号middle

:nospace: middle :nospace:

:any-non-whitespace:
:text1:sample2:
:@(1@#$@SD: :s:
:nospace::inbetween: because there are 2 colons in the middle
:nospace:middle:nospace:

我从这样的东西开始,但不完整

I'm starting with something like this but it's incomplete

/:(?!:)\S+:/gim

我正在尝试考虑所有可能发生的特殊情况

I'm trying to think of all the special cases that might possibly occur doing this. Maybe I'm overthinking it.

Twitch涉及很多表情符号,因此我不能使用表情符号unicode字符。正则表达式将找到匹配项并替换为标签

There's a lot of Twitch emotes involved so I can't use emoji unicode characters. The regex will find matches and replace with tags

推荐答案

我建议使用

:[^:\s]*(?:::[^:\s]*)*:

请参见 regex de mo 。它与 :(?:[^:\s] | ::)*: ,但效率更高一些,因为(?:.. | ...)* 部分是展开

See the regex demo. It is the same pattern as :(?:[^:\s]|::)*:, but a bit more efficient because the (?:..|...)* part is unrolled.

详细信息


  • -冒号

  • [^:\s] * -除以外的0多个字符:和空格

  • (?:-一个未捕获的量​​化组的开始:


    • :: -双冒号

    • [^:\s ] * -除和空格

    • : - a colon
    • [^:\s]* - 0+ chars other than : and whitespace
    • (?: - start of a quantified non-capturing group:
      • :: - double colon
      • [^:\s]* - 0+ chars other than : and whitespace

      这篇关于正则表达式来查找和替换冒号内的表情符号名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆