Put quotes around a variable string in JavaScript

29

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:

"'http://example.com'"

This question is tagged with javascript jquery string

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

The Best Answer is


51

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:

"http://example.com"

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


29

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: