My string is something like
var query = "@id >= 4 OR @id2 < 6 AND @id3 >= 5 AND @name = foo "
Now what I would like to do with this string is to reverse every “equality” test.
Replacing ‘ >=’ by ‘ <‘ , ‘ <‘ by ‘ >=’ and ‘ =’ by ‘ !=’ .
The result I want :
var reverseQuery = "@id < 4 OR @id2 >= 6 AND @id3 < 5 AND @name != foo "
We can’t use :
reverseQuery = query.replace(/>=/g, "<").replace(/</g, ">=").etc
Because the result of this would be
@id >= 4 OR @id2 >= 6 AND @id3 >= 5 AND @name != foo
Right ? So how to do this nicely ?
Thanks,
Use a single replace with a callback function that determines the replacement.
Demo: http://jsfiddle.net/Guffa/s2xj5/