Regex matching emoticons

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

    You want regex look-arounds regarding spacing. Another answer here suggested a positive look-ahead, though I'd go double-negative:

    (?

    While JavaScript doesn't support (?, look-behind can be mimicked:

    test_string.replace(/(\S)?(\:\)|\:\(|<3|\:\/|\:-\/|\:\||\:p)(?!\S)/,
                        function($0, $1) { return $1 ? $0 : replacement_text; });
    

    All I did was prefix your code with (? in front and suffix with(?!\S) in back. The prefix ensures you do not follow a non-whitespace character, so the only valid leading entries are spaces or nothing (start of line). The suffix does the same thing, ensuring you are not followed by a non-whitespace character. See also this more thorough regex walk-through.

    One of the comments to the question itself was suggesting \b (word boundary) markers. I don't recommend these. In fact, this suggestion would do the opposite of what you want; \b:/ will indeed match http:// since there is a word boundary between the p and the :. This kind of reasoning would suggest \B (not a word boundary), e.g. \B:/\B. This is more portable (it works with pretty much all regex parsers while look-arounds do not), and you can choose it in that case, but I prefer the look-arounds.

提交回复
热议问题