I was wondering how would you escape special characters in nodejs. I have a string $what$ever$ and I need it escaped like \$what\$ever\$ before i call a python script with it.
I tried querystring npm package but it does something else.
You can do this without any modules:
str.replace(/\\/g, "\\\\")
.replace(/\$/g, "\\$")
.replace(/'/g, "\\'")
.replace(/"/g, "\\\"");
Edit:
A shorter version:
str.replace(/[\\$'"]/g, "\\$&")
(Thanks to Mike Samuel from the comments)
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