You need to use a global regular expression for this. Try it this way:

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

Check out regex syntax and options for the replace function in Using Regular Expressions with JavaScript.

Answer from Willem D'Haeseleer on Stack Overflow
Discussions

I need help with escaping literal quotes
Tell us what’s happening: hey Your code so far var myStr = "I am a/'Double quoted\"string inside\"double quotes\"."; Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
June 19, 2019
Escaping Single and Double Quotes in a generated String - JavaScript - SitePoint Forums | Web Development & Design Community
Hi, I’ve been wrestling with this issue for a while and I’m hoping someone can help. I am building an administrative panel for one of my applications using ajax so I’m using lots of javascript to display information on the page. I’m having an issue with one piece in particular. More on sitepoint.com
🌐 sitepoint.com
0
April 23, 2006
Escaping both single and double quotes
Use the backticks and don't unnecessarily quote object keys. More on reddit.com
🌐 r/learnjavascript
4
2
December 26, 2022
Escape quotes in JavaScript - Stack Overflow
Possible duplicate of How do I escape a string inside JavaScript code inside an onClick handler?. ... You need to escape the string you are writing out into DoEdit to scrub out the double-quote characters. More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › js › js_strings.asp
JavaScript Strings
1 month ago - A JavaScript string is zero or more characters written inside quotes. ... let carName1 = "Volvo XC60"; // Double quotes let carName2 = 'Volvo XC60'; // Single quotes Try it Yourself »
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
I need help with escaping literal quotes - JavaScript - The freeCodeCamp Forum
June 19, 2019 - Tell us what’s happening: hey Your code so far var myStr = "I am a/'Double quoted\"string inside\"double quotes\"."; Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/…
🌐
javascript.com
javascript.com › learn › strings
JavaScript Strings: The Basic Methods and Functions | JavaScript.com
That means strings containing single quotes need to use double quotes and strings containing double quotes need to use single quotes. Alternatively, you can use a backslash \ to escape the quotation marks.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-escape-all-single-and-double-quotes-in-javascript
How to Escape All Single and Double Quotes in JavaScript? - GeeksforGeeks
November 8, 2024 - To escape all single and double quotes in a string using JavaScript, you can use a regular expression with the replace method.
🌐
SitePoint
sitepoint.com › javascript
Escaping Single and Double Quotes in a generated String - JavaScript - SitePoint Forums | Web Development & Design Community
April 23, 2006 - Hi, I’ve been wrestling with this issue for a while and I’m hoping someone can help. I am building an administrative panel for one of my applications using ajax so I’m using lots of javascript to display information on…
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › escaping both single and double quotes
r/learnjavascript on Reddit: Escaping both single and double quotes
December 26, 2022 -

Hey,

I'm creating an object which contains user-typed content and the very same object will be printed / embeded somewhere else after. The tricky thing is :

- The object uses "xxx" for strings

- The place where it will be embeded as a text uses '{"mykey" : "myobject"}'

The object's creation is done using js ; the embed is in a bash environment. These are two separate scripts. So, basically, I want to escape all quotes and single quotes but also avoid escaping a user-typed quote like in this case \', which would output \\' and unescape the quote by escaping the escape itself. When I build my string, it seems that the quotes are successfully replaced with escaped ones. But then when I return the string, is seems that the escape is taken in account so the escapes disappears. What I'm trying to explain is that apparently when I return the escaped string, JS uses it to return the unescaped version.

How can I escape quotes and keep the string escaped for later use ? thanks

🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-escape-quotes-in-string
How to Escape Quotes in a String using JavaScript | bobbyhadz
March 2, 2024 - To escape a single or double quote in a string, use a backslash `\` character before each single or double quote in the contents of the string.
🌐
GitHub
gist.github.com › getify › 3667624
escape all (not-already-escaped) double-quote chars in a string · GitHub
is there an escaped (something) ? escape(something) : is the char a " ? escape(") ... the replace escapes in any case but only (soemthing) OR (quote) are actually placed there: about empty, ignored, matches ... maybe better ... ... In which case the result is false, which is both expected and correct, due to JavaScript string escaping rules. @getify, the regex works by matching one of two alternatives: A backslash followed by any character ([\s\S] is used instead of a dot so that escaped newlines also count), or... A double quote character, on its own.
🌐
DEV Community
dev.to › theudemezue › how-to-escape-quotes-in-javascript-1hlh
How To Escape Quotes In JavaScript - DEV Community
March 8, 2025 - Always double-check your strings when integrating external code into your project. Let’s walk through a few practical examples to see how these methods work in real life. Imagine you want to display the sentence: John said, "JavaScript is fun!" You can write it like this: let message = 'John said, "JavaScript is fun!"'; console.log(message); Or you could use double quotes and escape ...
🌐
Log4JavaScript
log4javascript.org › home › js-framework › demystifying escaping quotes in javascript
Javascript Escape Quotes: Mastering - JavaScript For-Log
September 20, 2023 - When you need to include both single and double quotes within a string, rely on the escape character () to make it work. var str1 = "The book \"World\" is on the table. I'm going to read."; var str2 = 'The book "World" is on the table.
🌐
Codecademy
codecademy.com › forum_questions › 5130907931ae6dec410085c8
how to print double quotes inside a string in javascript? | Codecademy
You have to use an escape character. This basically means you put a backslash before the character you want to esacpe. Per your example… ... This should do the trick. Hope this helps!
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-use-escape-characters-to-correctly-log-quotes-in-a-string-using-javascript
How to use Escape Characters to Log Quotes in JavaScript? - GeeksforGeeks
September 9, 2024 - By placing a backslash (`\`) before a quote, you can ensure that the quote is treated as part of the string rather than as a delimiter. This is essential for correctly logging strings that contain both single and double ...