Put quotes around a variable string in JavaScript

后端 未结 12 1976
时光说笑
时光说笑 2020-12-05 02:46

I have a JavaScript variable:

var text = \"http://example.com\"

Text can be multiple links. How can I put \'\' around the variable string?<

12条回答
  •  青春惊慌失措
    2020-12-05 03:30

    In case of array like

    result = [ '2015',  '2014',  '2013',  '2011' ],
    

    it gets tricky if you are using escape sequence like:

    result = [ \'2015\',  \'2014\',  \'2013\',  \'2011\' ].
    

    Instead, good way to do it is to wrap the array with single quotes as follows:

    result = "'"+result+"'";

提交回复
热议问题