javascript - Replace string everywhere except if its within quotes -


i replace :) :d, except if within quotes "

example 1:

hey man :d, how're you? :) friend told me "this can't true :)" 

becomes

hey man :d, how're you? :d friend told me "this can't true :)" 

as see, :) not replaced if it's enclosed ". if condition wouldn't there, quite simple, right? using javascript (jquery) this.

if not plainly possible regex, alternate suggestion?

assuming no double quote unbalanced, regex should work you:

:\)(?=(?:(?:[^"]*"){2})*[^"]*$) 

explanation: regex using positive lookahead matching 0 or more occurrences of pair of some text until double quote found i.e. ([^"]*"){2} on right hand side (rhs) of every match of :).

which in simple term means replace :) if outside double quotes since matches inside double quotes have odd number of [^"]*" matches on rhs.

live demo: 1. http://www.rubular.com/r/3aixzy5byr

live demo: 2. http://ideone.com/c679nw


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -