Put quotes around a variable string in JavaScript


I have a JavaScript variable:

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

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

I want the strings to, for example, look like this:


This question is tagged with javascript jquery string

~ Asked on 2012-03-15 06:07:08

The Best Answer is


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

Whatever your text, to wrap it with ", you need to put them and escape inner ones with \. Above will result in:


~ Answered on 2012-03-15 06:09:48


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

text = "'"+text+"'";

Would attach the single quotes (') to the front and the back of the string.

~ Answered on 2012-03-15 06:12:23

Most Viewed Questions: