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.
Comments
Post a Comment