Regex matching emoticons

后端 未结 4 1880
春和景丽
春和景丽 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:23

    Make a positive look-ahead for a space

    ([\:\<]-?[)(|\\/pP3D])(?:(?=\s))
     |       |      |         |
     |       |      |         |
     |       |      |         |-> match last separating space
     |       |      |-> match last part of the emot
     |       |-> it may have a `-` or not 
     |-> first part of the emoticon
    

    Since you're using javascript, and you don't have access to look arounds:

    /([\:\<]-?[)|\\/pP3D])(\s|$)/g.exec('hi :) ;D');
    

    And then just splice() the resulting array out of its last entry (that's most probably a space)

提交回复
热议问题