Say i have a string that i need to evaluate in javascript such as :
window.some_class.update( 'Runner', 'update_runner', '{"runner":{"id":"1","name":"Nin's Lad" } }');
In order for eval() to evaluate it, i need to escape the apostrophe in runner_name (Nin’s Lad). Is this possable with regex? I dont want to escape the single quotes around Runner and update_runner. I’d just like to escape any single quotes inside double quotes.
Thanks,
This works for your specific case, but I’m sure there are some corner cases someone will point out:
yourString = yourString.replace(/"([^"]*)'([^"]*)"/g, "$1\\'$2");Also, I’d like to point you to the last paragraph of this page:
If you’re using
eval, there’s probably a better way to accomplish your goal.