Regular expression for finding and replacing emoji names inside colons

I am trying to write a regular expression (for the JavaScript regex engine) that I can use to find and replace text for emoji names in colons. Like in Slack or Discord when typing :smiley-face:, and it replaces it when sending a chat. I only target text nodes, so I don't need to worry about another html inside the text.

Is it possible to write a regular expression that could meet all of the following rules? (text highlighted in monoscopic blocks = regular expressions)

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

I start with something like this, but it's incomplete

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

I try to think of all the special cases that may occur in this case. Maybe I'm overdoing it.

There are a lot of Twitch emotions there, so I cannot use the unicode characters from emoji. The regular expression will find matches and replace with tags

+4
source share
3 answers

I suggest using

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

See the regex demo . This is the same template as :(?:[^:\s]|::)*:, but a little more effective, because part was (?:..|...)* rolled out .

More details

  • : - colon
  • [^:\s]*- characters 0+ except :and spaces
  • (?: - beginning of a quantitative group not related to capture:
    • :: - double colon
    • [^:\s]*- characters 0+ except :and spaces
  • )*- end of grouping, repeating 0 or more times (due to quantifier *)
  • : - colon.
+2
source

:(::|[^:\n])+:

, ,

  • (::)
  • , , .

, Wiktor ( ) (). , , ;)

, regex101.

0

- ?

(:(?![\n])[()#$@-\w]+:)

Demo,, unallowed characters (?![\n]), allowed characters [()#$@-\w]

0

Source: https://habr.com/ru/post/1696000/


All Articles