Don't put the regular expression in quotes, that makes it an ordinary string.
var s = "abcdwq'xx'x";
console.log(s.replace(/(["'])/g, "\\$1"));
Also, you were escaping the ] that ends [.
If you just want to escape single quotes, you don't need the brackets or capture group. Just do:
var s = "abcdwq'xx'x";
console.log(s.replace(/'/g, "\\'"));
Answer from Barmar on Stack Overflowjavascript - Escape quotes in a string with backslash - Stack Overflow
javascript - How do you replace backslash double quote in a string? - Stack Overflow
I am having trouble creating the quote
google chrome - Javascript is adding a slash between Parenthesis and Quote - Stack Overflow
What I have is scarcely better, but it does handle escaped backslashes too:
>>> var v= 'a\\"b'
>>> v
"a\"b"
>>> v.replace(/(\\*)(")/g, function(x) { var l = x.length; return (l % 2) ? x : x.substring(0,l-1) + '\\"' } )
"a\\"b"
>>> var v= 'a\\\\"b'
>>> v
"a\\"b"
>>> v.replace(/(\\*)(")/g, function(x) { var l = x.length; return (l % 2) ? x : x.substring(0,l-1) + '\\"' } )
"a\\"b"
If there are an odd number of slashes before a quote (1, 3, 5), the quote is already escaped; an even number (including zero), in needs escaping.
Made all the harder to read by the necessary of escaping the slashes in input and by the inability of the colorizer to understand the regexp expression...
Of course, you probably shouldn't even be doing this. If you have a raw string and you need something you can pass to (e.g.) eval, consider $.toJSON.
This worked for me:
var arf = input.replace(/(^|[^\\])"/g, '$1\\"');
It says, replace a quote, when preceded by beginning-of-string or anything-other-than-backslash, with backslash followed by quote.
var myVar = '\"Things You Should Know\"';
document.write(myVar.replace(/\"/g, '|'));
The \ escapes the next character so your string only reads "Things You Should Know"
Your string doesn't have the sequence backslash double-quote in it. The backslash is an escape character so \" means " (this is useful in strings that are delimited by double quote characters).
If you did have that sequence in your string (by escaping the backslash characters):
var myVar = '\\"Things You Should Know\\"';
… then you could do it with:
var modifiedString = myVar.replace(/\\"/g, "|");
Try this:
text = text.toString().replace(/"/g, '\\"')
Or this:
text = text.toString().replace('"', '\\"')
I have a small suggestion based on antyrat's answer.
text = text.toString().replace(/\\"/g, '"').replace(/"/g, '\\"');
This extra step will replace all the \" to " first, and then replace all the " back to \". It will help when your current string contains a combination of \" and ", especially when the string is a result from JSON.stringify()
replace works for the first quote, so you need a tiny regular expression:
str = str.replace(/'/g, "\\'");
Following JavaScript function handles ', ", \b, \t, \n, \f or \r equivalent of php function addslashes().
function addslashes(string) {
return string.replace(/\\/g, '\\\\').
replace(/\u0008/g, '\\b').
replace(/\t/g, '\\t').
replace(/\n/g, '\\n').
replace(/\f/g, '\\f').
replace(/\r/g, '\\r').
replace(/'/g, '\\\'').
replace(/"/g, '\\"');
}