var test = [
    'Mercu"ry', 'Mercu,ry', 'Mer"cu,ry', 'Mercury'
];


for (x in test) {
    var s = test[x];
    if (s.indexOf('"') != -1) {
        s = s.replace(/"/g, '""');
    }

    if (s.match(/"|,/)) {
        s = '"' + s + '"';
    }

    alert(s);
}

Test: http://jsfiddle.net/ZGFV5/

Try to run the code with Mer""cury :)

Answer from Seyeong Jeong on Stack Overflow
🌐
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.
Discussions

javascript - Regular expression to escape double quotes within double quotes - Stack Overflow
I need a regex to replace any double quotes between the opening and closing " with a \". ... You need to fix the problem at whatever is generating the JSON. – Explosion Pills Commented May 15, 2013 at 14:40 ... First and last before/after the ':'. ... I concur with @ExplosionPills, you need to use code that already knows how to build JSON if you can...those quotes should be escaped ... More on stackoverflow.com
🌐 stackoverflow.com
May 15, 2013
How do I replace a double-quote with an escape-char double-quote in a string using JavaScript? - Stack Overflow
Using this method it's easy to save objects as strings which can be useful in certain situations. without the regex, the JSON will be escaped and the queries will fail. 2020-10-16T08:49:31.09Z+00:00 ... This fixed the issue. Thanks 2022-01-19T02:16:54.15Z+00:00 ... I've run into a double quote ... More on stackoverflow.com
🌐 stackoverflow.com
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
javascript - Escape double quotes within double quotes with Regex[JS] - Stack Overflow
Thanks! using System; using ... Console.WriteLine(Regex.Replace(s, "\"((?:\"[^\"]*\"|[^\"])*?)\"(?=[:},])(?=(?:\"[^\"]*\"|[^\"])*$)", delegate (Match m) { string group = m.Groups[1].Value; return "\"" + group.Replace("\"", "\\\"") + "\""; })); } } ... The December 2024 Community Asks Sprint has been moved to March 2025 (and... Stack Overflow Jobs is expanding to more countries · 1 Escape apostrophes inside double quoted strings (Javascript... More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
🌐
GitHub
gist.github.com › getify › 3667624
escape all (not-already-escaped) double-quote chars in a string · GitHub
@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.
🌐
JavaScript.info
javascript.info › tutorial › regular expressions › alternation (or) |
Find quoted strings
Then if we have a backslash \\ (we have to double it in the pattern because it is a special character), then any character is fine after it (a dot). Otherwise we take any character except a quote (that would mean the end of the string) and a backslash (to prevent lonely backslashes, the backslash is only used with some other symbol after it): [^"\\] ... let regexp = /"(\\.|[^"\\])*"/g; let str = ' ..
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript escape double quotes in a string | example code
JavaScript escape double quotes in a string | Example code
June 24, 2021 - Use replace method with regex to escaping double quotes in JavaScript from a string in a variable. The replace() method returns a new string
🌐
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
🌐
javaspring
javaspring.net › blog › how-do-i-replace-a-double-quote-with-an-escape-char-double-quote-in-a-string-using-javascript
How to Replace Double Quotes with Escape Characters in JavaScript Strings and SQL String Optimization Tips — javaspring.net
The most direct way to escape double quotes in JavaScript is with the String.replace() method, paired with a regular expression to target all occurrences. Use the regex /"/g to match all double quotes in the string (the g flag ensures "global" ...
🌐
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.
🌐
Reddit
reddit.com › r/clojure › escaping of quotation marks in regular expressions?
r/Clojure on Reddit: Escaping of Quotation Marks in Regular Expressions?
December 1, 2012 -

Just a small question about quotation marks in regular expression patterns since it's causing me a little bit of confusion. Why are those two different:

(.pattern #"\"")             ;=> "\\\""
(.pattern (re-pattern "\"")) ;=> "\""

when evaluation of

(re-pattern "\"")  

yields

#"\""

?

(Or the other way round: why is (re-pattern "\"") the same as (re-pattern "\\\"") ?)

Do quotation marks really have to be escaped? If so, what exactly is their special meaning?

They do match the same strings, I just happen to need the pattern string for processing and now I can't be sure when to remove a backslash before a quotation mark...

🌐
GitHub
gist.github.com › MirzaLeka › fe33f850d783997181d97dc02cefc000
Replace double quotes with single quotes | Javascript · GitHub
Although MongoDB will add an escape character if you use double quotes, using double quotes multiple times in the same string will throw an error. To avoid it, I found this handy code snippet that will replace all double quotes with single ones using regex.
🌐
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

🌐
Surma.dev
surma.dev › things › regexp-quote
My most useful RegExp trick — surma.dev
May 2, 2018 - HTML tags are a funny one because the (infinite number of) escape sequences for “>” don’t contain the character “>” (like &gt;). Because of this most simple RegExps like /<[^>]*>/gus work just fine. ... To handle this case, we have to use our new trick twice. The only way a closing tag “>” can appear in an HTML tag is inside a string. So our first alternative will accept anything that is not a closing tag or a double quote.
🌐
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/…