I want to escape single and double quotes with a backslash in one line rather than two.
Example for single quote:
str = str.replace(/'/g, "\\'");
Is there a way to do this at the same time for double quotes included?
Sniffer Answered this very well below, and I ended up escaping all characters we needed as follows:
str = str.replace(/(['"&:;])/g, "\\$1");
Thanks again Sniffer for the quick response!
Try this:
str = str.replace(/(['"])/g, "\\$1");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With