How to add single quote in the variable in Javascript?

前端 未结 4 1095
深忆病人
深忆病人 2020-12-11 01:33

I have variable var str as following:

var str = ;

I would like to make it as below

4条回答
  •  Happy的楠姐
    2020-12-11 02:27

    I think that you want the semicolon outside the string literal:

    var quote_str = '';
    

    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 = '\'\'';
    

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

    If you already have a string, and want to add apostrophes around it, you concatenate strings:

    var quote_str =  "'" + str + "'";
    

提交回复
热议问题