Put quotes around a variable string in JavaScript

后端 未结 12 1959
时光说笑
时光说笑 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:16

    To represent the text below in JavaScript:

    "'http://example.com'"
    

    Use:

    "\"'http://example.com'\""
    

    Or:

    '"\'http://example.com\'"'
    

    Note that: We always need to escape the quote that we are surrounding the string with using \

    JS Fiddle: http://jsfiddle.net/efcwG/

    General Pointers:

    • You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

    Example

    var answer="It's alright";
    var answer="He is called 'Johnny'";
    var answer='He is called "Johnny"';
    
    • Or you can put quotes inside a string by using the \ escape character:

    Example

    var answer='It\'s alright';
    var answer="He is called \"Johnny\"";
    
    • Or you can use a combination of both as shown on top.

    http://www.w3schools.com/js/js_obj_string.asp

提交回复
热议问题