Double Quote in JavaScript String

Double quote in JavaScript string

Use single quotes.

error += '<li> this is not the name "....." </li>\n';

Or escape the double quotes.

error += "<li> this is not the name \".....\" </li>\n";

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.

How to display double quotes in JavaScript

Hello everyone thanks for suggestions. My issue has been solved now. I created one private method to which I passed my required parameters which contains double quotes in it. Here is the method.

 HTMLEncode:function(str){
var i = str.length,
aRet = [];

while (i--) {
var iC = str[i].charCodeAt();
if (iC < 65 || iC > 127 || (iC>90 && iC<97)) {
aRet[i] = '&#'+iC+';';
} else {
aRet[i] = str[i];
}
}
return aRet.join('');
},

The above method will convert double quote into the required format to display it on web page.
I referred one of the stackoverflow page for this. Thanks. Hope this will help others.

Add double quotes to string which is stored in variable

var audioUrl = "\""+ data.url+  "\""; 

Whatever your get audioUrl and you want to wrap it with ", you need to put them and escape inner ones with . Above will result in:

 "http://xxxxx/xxx/xx-xx-123.m4a"

OR if you are using the single quotes then no need to use the escape character.

var audioUrl = '"'+ data.url+  '"'; 

Regex for double quotes inside string

It is not a problem: just match these strings and use a replace callback method to replace double apostrophes with single ones:

var expression = /'([^']*(?:''[^']*)*)'/g;
var replacer = function(m, g1){
return g1 ? "'" + g1.replace(/''/g, '\'') + "'" : "''";
}

See this updated fiddle

The '([^']*(?:''[^']*)*)' regex only matches the single quoted string literals capturing the contents in-between the single quotes.

Pattern matches:

  • ' - leading single quote
  • ([^']*(?:''[^']*)*) - Group 1 capturing any 0+ characters inside the '':

    • [^']* - 0+ characters other than a single quote
    • (?:''[^']*)* - 0+ sequences of:
    • '' - two consecutive apostrophes
    • [^']* - 0+ characters other than a single quote
  • ' - closing single quote

So, when we perform the replacement, we check of Group 1 matches and is not empty. If it is, we just return '', and if not, we replace double apostrophes with single ones with the help of the "'" + g1.replace(/''/g, '\'') + "'" inside the replace callback.

Difference between single quotes and double quotes in Javascript

You'll want to use single quotes where you want double quotes to appear inside the string (e.g. for html attributes) without having to escape them, or vice versa. Other than that, there is no difference.

However, note that JSON (JavaScript Object Notation) only supports double quoted strings.

wrapping a javascript variable with double quotes