Find all ":)" that are not surrounded by any character

I am trying to change all the ":)" (no quotes) in the photo. To make a match, the emoji must be surrounded by spaces (or be at the beginning or end of a line).

My attempt: /(?:^|\s)\:\)(?:$|\s)/g

if the smiley is (at the beginning of the line or a space before it) and (is at the end of the line or has a space after it);

A line like this works great: ":) x :) x :)" but there is no such line: ":) :) :) :) (every second smiley changes).

As I understand it, the first smiley is matched with a space after it, and the next smiley is neither at the beginning of the line nor in white space. I'm new to regex and can't figure out how to fix my logic :)

PS maybe there is a shortcut to find a template that is not surrounded by any character? ( \b

and \b

won't work for that)

+3


source to share


1 answer


How about regex

(?:\s|^):\)(?=\s|$)

      

Example: http://regex101.com/r/zY9xA3/2

A problem with /(?:^|\s)\:\)(?:$|\s)/g

  • \s

    after is :)

    consumed by the regex engine, which for the second :)

    we can't have a precedent\s



Decision

Use positive lookahead so that the space after is not used by the regular expression.

  • (?=\s|$)

    Forward asserts to :)

    be followed by a space or end of line. But don't use the character.

Changes made

  • \:

    until :

    you don't need to avoid:

  • (?:$|\s)

    not capturing the group to look forward positively (?=\s|$)

+4


source







All Articles