javascript - Match a condition RegEx -
i need able strip out condition in string, here is:
(("${operation}" == "fixed") && ("-" == "-"))
i'd able replace "${operation}" == "fixed"
""
or between () has ${} in it. in other words if there ${} on left hand side of condition want rid of condition entirely.
in general, regular expression not right approach here. there lots of cases valid javascript syntax, won't fit nicely regular expression. however, here's 1 way it:
var code = '(("${operation}" == "fixed") && (thing1 == thing2))'; code = code.replace(/"[^"]*\$\{[^}]*\}[^"]*"\s*==\s*"[^"]*"/, 'true');
that handles double quotes. if need handle both types of quotes, expression more complicated. if need handle both types of quotes and strings might contain quotes, regular expression isn't going work.
if can think of different way solve problem, should. example - string come from? can data in different format?
also, sounds you're setting string call eval
. that's bad idea if contains kind of user input.
Comments
Post a Comment