javascript - find all ":)" that are not surrounded by any character -


i'm trying change ":)" (without quotes) pictures. make match, smiley must surrounded spaces (or @ beginning or end of string).

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

if smiley (at beginning of string or has white-space before it) , (is @ end of string or has white-space after it);

a string works fine: ":) x :) x :)" such string not: ":) :) :) :)" (every second smiley changed).

as understand, first smiley matched space after , next smiley neither @ beginning of string or has white-space anymore. i'm new regular expressions , can't figure out how fix logic :)

p.s. maybe there shortcut find pattern not surrounded character? (\b , \b not work that)

how regex

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

example : http://regex101.com/r/zy9xa3/2

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

  • the \s after :) consumed regex engine second :) cannot have presceding \s

solution

use positive ahead the space after not consumed regex.

  • (?=\s|$) ahead asserts :) followed space or end of string. wont consume character.

changes made

  • \: : need not escape the :

  • (?:$|\s) non capturing group positive ahead (?=\s|$)


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -