18

I have variable var str as following:

var str = <option value="1">tea</option>;

I would like to make it as below

var quote_str = '<option value="1">tea</option>;'

Is there anyone can help me? Thanks in advance!

Edit:

I have tried the following code,however, it's not correct.

var quote_str =  'str';
4
  • 7
    what's stopping you? Commented May 28, 2012 at 15:59
  • 3
    Use a text editor and make the changes. Commented May 28, 2012 at 16:00
  • 1
    Move your ";" outside the string: var quote_str = '<option value="1">tea</option>'; Commented May 28, 2012 at 16:02
  • 1
    If you literally hav var str = <option value="1">tea</option>; inside your source, then there is no way to fix this programmatically with JavaScript, since the code is not valid JS code. You have to fix the source. Commented May 28, 2012 at 17:00

5 Answers 5

51

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 + "'";
Sign up to request clarification or add additional context in comments.

2 Comments

var quote_str = '<option value="1">tea</option>'; or if you want to preserve single quotes without escaping anything backticks are your friend & useful var quote_str = `'<option value="1">tea</option>'` . With backticks you can use variables e.g. replace tea with ${tea} which can be a variable defined above that resolves into set value. Backticks also handle well multilines without the need for \n.
Can you share the source where you learned this?
5

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>;'`

3 Comments

You need to escape the quotation marks in the string delimited by quotation marks: "'<option value=\"1\">tea</option>;'"
can you share the source where you learned this?
@Shad - MDN is a good resource
2

You can escape characters in Javascript with the \. If that's your issue

Comments

2

We can use the backslash () escape character to prevent JavaScript from interpreting a quote as the end of the string.

The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.

Using this method, we can use apostrophes in strings built with ".

'We\'re safely using an apostrophe in single quotes.' We can also use quotation marks in strings built with ".

"Then he said, \"Hello, World!\"";

Comments

0

In my case, i'm unable to use the notation of ${} in rendered Javascript inside Python Mako Templates as it's already using ${} for rendering variables in Mako:

# mako template somewhere
var quote_str = `'${str}'`;

So i just wrote a small function:

# app.js ( a real Javascript file )
function singlequote(text) {
    return `'${text}'`;
}

And then I use:

# mako template somewhere
var quote_str = singlequote(str);

# So i'm able to also use something like:
let btn = '<button type="button" onclick="update(' + singlequote(myid) + "," + singlequote(mystate) + ')"> Update </button>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.