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 Overflow
🌐
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, e.g. 'that\'s it'. ... Copied!const escapeSingle = 'it\'s a string'; console.log(escapeSingle) // 👉️ ...
🌐
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 »
Discussions

javascript - Escape quotes in a string with backslash - Stack Overflow
I have a string: " \" ". I would like to escape all unescaped double-quotes, i.e. add a backslash before " if it's note there. More on stackoverflow.com
🌐 stackoverflow.com
javascript - How do you replace backslash double quote in a string? - Stack Overflow
In javascript, both ways work, however. Anyway, to search for a backslash followed by quote, you can use /\\"/g since you must use a backslashed backslash if you want the regular expression to look for a backslash. ... Sign up to request clarification or add additional context in comments. ... Your string doesn't have the sequence backslash double... More on stackoverflow.com
🌐 stackoverflow.com
I am having trouble creating the quote
Tell us what’s happening: Your code so farI var myStr = "I am a "/double quoted/" string inside\"double quotes/"."; Your browser information: User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13421.89.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
March 4, 2021
google chrome - Javascript is adding a slash between Parenthesis and Quote - Stack Overflow
So while you got around the need of escaping by using single quotes, JavaScript refuses to choose and always uses double quotes, and then has to use the double quotes to make sure the result is not garbage ("By.XPath("//test")"). ... Sign up to request clarification or add additional context ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Codecademy
codecademy.com › forum_questions › 5130907931ae6dec410085c8
how to print double quotes inside a string in javascript? | Codecademy
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! ... In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language. Beginner Friendly.Beginner Friendly4 Lessons4 Lessons ... Learn how to use JavaScript — a powerful and flexible programming language for adding ...
🌐
WebDeveloper.com
webdeveloper.com › community › 48807-how-to-add-backslashes-before-single-and-doublequot-quotes-in-a-string
How to add backslashes(”) before single (‘) and double(") quotes in a string?
If you insert the string in doublt qoutes, like in addslashes("s't'r'i'n'g"); then it will insert a backslash before every (') it finds, but if it finds (") instead it treats it as the end of the string and so the function terminates.
🌐
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.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
I am having trouble creating the quote - JavaScript - The freeCodeCamp Forum
March 4, 2021 - Tell us what’s happening: Your code so farI var myStr = "I am a "/double quoted/" string inside\"double quotes/"."; Your browser information: User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13421.89.0) AppleWebKit/537…
🌐
Blogger
js-code.blogspot.com › 2010 › 12 › insert-backslash-before-double-quote.html
JavaScript Tips: Insert Backslash Before a Double Quote
December 10, 2010 - newstr = '\\"'; The last line searches the string and if a double quote is found it is replaced by our newstr pattern. return str.replace(reg,newstr); Here is a similar function that inserts a backslash before a single quote if it is contained in the string argument. function insertBacksla...
🌐
GeeksforGeeks
geeksforgeeks.org › 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 quotes.
🌐
Online String Tools
onlinestringtools.com › escape-string
Slash-escape a String – Online String Tools
This tool implements PHP's addslashes function in JavaScript. It works entirely in your browser and what it does is it adds slashes to a string to escape special characters, such as backslashes, tabs, newlines, single quotes, and double quotes. Internally, it takes the input string and checks if the current character is special and if it is, then it adds a backslash before it.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-work-with-strings-in-javascript
How To Work with Strings in JavaScript | DigitalOcean
August 24, 2021 - In JavaScript, there are three ways to write a string — they can be written inside single quotes (' '), double quotes (" "), or backticks (` `).
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
I Dont understand it - JavaScript - The freeCodeCamp Forum
March 2, 2021 - So this is what i have var myStr = ‘I am a “double quoted” string inside “double quotes”.’; And it wants me to You should use two double quotes ( " ) and four escaped double quotes ( \" ). Variable myStr should contain the string: I am a "double quoted" string inside "double quotes". ...
🌐
DEV Community
dev.to › manikbajaj › character-literals-in-javascript-a-few-unused-ones-as-well-21g9
Escape Sequence in JavaScript - A Few Unused Ones as Well - DEV Community
February 21, 2021 - In Javascript, character literals use an escape character '\', which is a backslash like so followed by the character you want to be treated as a string value by JavaScript, so a double quote in our case.
🌐
codestudy
codestudy.net › blog › how-to-escape-double-quotes-in-json
How to Escape Double Quotes in JSON: Fix Backslashes Showing in HTML Rendering — codestudy.net
The JSON specification (RFC 8259) defines the backslash (\) as the escape character. To include a double quote inside a JSON string, prefix it with \: ... Now the inner " is escaped as \", and JSON parses the string correctly.
🌐
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 - Escaping: Adding a backslash (\) before quotes ensures they are treated as literal characters rather than string delimiters. Practical Usage: This is commonly used when preparing strings for inclusion in HTML or JavaScript code to avoid syntax ...