How do I add slashes to a string in Javascript?

TIMEX picture TIMEX · Feb 3, 2010 · Viewed 122.6k times · Source

Just a string. Add \' to it every time there is a single quote.

Answer

Kobi picture Kobi · Feb 3, 2010

replace works for the first quote, so you need a tiny regular expression:

str = str.replace(/'/g, "\\'");