How to add single quote in the variable in Javascript?

92,655

Solution 1

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 + "'";

Solution 2

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

Solution 3

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

Solution 4

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!\"";

Share:
92,655
Acubi
Author by

Acubi

Updated on October 04, 2020

Comments

  • Acubi
    Acubi over 3 years

    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';
    
  • Guffa
    Guffa almost 12 years
    You need to escape the quotation marks in the string delimited by quotation marks: "'<option value=\"1\">tea</option>;'"
  • webcoder.co.uk
    webcoder.co.uk over 2 years
    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.
  • Shad
    Shad almost 2 years
    can you share the source where you learned this?
  • Shad
    Shad almost 2 years
    Can you share the source where you learned this?
  • Eliran Malka
    Eliran Malka almost 2 years
    @Shad - MDN is a good resource