If possible, I'd use ES6 syntax for this:
`"${data.url}"`
Answer from Jonah on Stack OverflowIf possible, I'd use ES6 syntax for this:
`"${data.url}"`
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+ '"';
Your code is vulnerable to string injection! What if myName contains a quote?
Instead, you should use JSON.stringify
var myName = 'John"abc';
'"' + myName + '"'; // "John"abc"
JSON.stringify(myName); // "John\"abc"
You may want to escape U+2028 and U+2029 too.
The plus sign is used to concate the strings and the backslash is used to tell JS "hey, this is not the ending doble quote mark, I want the actual sign". You can also use both doble and single quote mark in your favor.
var myName = 'John';
document.write('"' + myName + '"');
Js Fiddle
html - Double quote in JavaScript string - Stack Overflow
Javascript quotes within a strings
How to add double quotes in a string ? | OutSystems
string - wrapping a javascript variable with double quotes - Stack Overflow
Copyvar text = "\"http://example.com\"";
Whatever your text, to wrap it with ", you need to put them and escape inner ones with \. Above will result in:
Copy"http://example.com"
Copyvar text = "http://example.com";
text = "'"+text+"'";
Would attach the single quotes (') to the front and the back of the 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";
Use single quotes as your string delimiters:
if (i == 0) {
error += '<li> this is not the name "....." </li>\n'
}
If you have single quotes in the string, delimit it with double quotes.
If you have double quotes in the string, delimit it with single quotes.
If you need to use both types in the string, escape whatever delimiter you have chosen by prefixing it with a \.
let currentType = "hello";
let objProp = "world";
let value = "hi";
let a = `${currentType} ${objProp} = "${value}";`
console.log(a)
Just use the double quote surrounding your ${value}
UPDATES:
Just to try out to prove that it can supports double quoted string as below
let value = '"hi"';
let a = `${value}`;
console.log(a)
let value2 = "\"hi\"";
let a2 = `${value2}`;
console.log(a2)
`${currentType} ${objProp} = ${JSON.stringify(value)};`
Using JSON.stringify will do the right thing for all JS primitives, quoting strings, and correctly formatting objects and arrays.
EDIT because so many of other answerers seem to be missing the point:
let currentType = 'string';
let objProp = 'actor';
let value = 'Dwayne "The Rock" Johnson';
let bad = `${currentType} ${objProp} = "${value}";`
console.log(bad);
// string actor = "Dwayne "The Rock" Johnson";
let good = `${currentType} ${objProp} = ${JSON.stringify(value)};`
console.log(good);
// string actor = "Dwayne \"The Rock\" Johnson";
var testStr='World';
tUserBlock.setAttribute('onclick', 'javascript:alert("Hello '+testStr+'")');
However this is a poor way of binding click events to elements. Instead, look into the element.addEventListener method - that is the preferred way of binding. There's various reasons for this - one is that you are not limited to just one event of each type (as you are with your current approach). Another is that it keeps code out of your mark-up.
This is the right way of binding events (no need to use setAttribute):
var testStr='World';
tUserBlock.onclick = function() { alert("Hello" + testStr) };
If you still insist on your original way, this is the way of doing it:
var testStr='World';
tUserBlock.setAttribute('onclick', 'javascript:alert("Hello ' + testStr + '")');
I think that you want the semicolon outside the string literal:
var quote_str = '<option value="1">tea</option>';
If you mean that you want apostrophe characters inside the string also, you can use \' to put an apostrophe in a string delimited by apostrophes:
var quote_str = '\'<option value="1">tea</option>\'';
You can also use quotation marks to delimit the string. Then you don't have to escape the apostrophes, but you have to escape the quotation marks:
var quote_str = "'<option value=\"1\">tea</option>'";
If you already have a string, and want to add apostrophes around it, you concatenate strings:
var quote_str = "'" + str + "'";
Escape each single quote with a back-slash:
var quote_str = '\'<option value="1">tea</option>;\''
…or wrap the string in quotes of a different kind (i.e. double quotes), but be sure to escape the inner double quotes as to not unintentionally close the string:
var quote_str = "'<option value=\"1\">tea</option>;'"
late update: now we have template literals, so the whole thing becomes a breeze:
var quote_str = `'<option value="1">tea</option>;'`