Regex matching emoticons

后端 未结 4 1879
春和景丽
春和景丽 2020-12-10 07:06

We are working on a project where we want users to be able to use both emoji syntax (like :smile:, :heart:, :confused:,:stuck_ou

4条回答
  •  余生分开走
    2020-12-10 07:33

    Match emoji first (to take care of the :pencil: example) and then check for a terminating whitespace or newline:

    (\:\w+\:|\<[\/\\]?3|[\(\)\\\D|\*\$][\-\^]?[\:\;\=]|[\:\;\=B8][\-\^]?[3DOPp\@\$\*\\\)\(\/\|])(?=\s|[\!\.\?]|$)
    

    This regex matches the following (preferring emoji) returning the match in matching group 1:

    :( :) :P :p :O :3 :| :/ :\ :$ :* :@
    :-( :-) :-P :-p :-O :-3 :-| :-/ :-\ :-$ :-* :-@
    :^( :^) :^P :^p :^O :^3 :^| :^/ :^\ :^$ :^* :^@
    ): (: $: *:
    )-: (-: $-: *-:
    )^: (^: $^: *^:
    <3 

    It also supports terminal punctuation as a delimiter in addition to white space.

    You can see more details and test it here: https://regex101.com/r/aM3cU7/4

提交回复
热议问题