Reputation: 2367
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'"
Upvotes: 40
Views: 236339
Reputation: 543
Another easy way to wrap a string is to extend the Javascript String prototype:
String.prototype.quote = function() { return "\"" + this + "\""; };
Use it like this:
var s = "abc";
console.log( "unwrapped: " + s + ", wrapped: " + s.quote() );
and you will see:
unwrapped: abc, wrapped: "abc"
Upvotes: 1
Reputation: 197
You can add these single quotes with template literals:
var text = "http://example.com"
var quoteText = `'${text}'`
console.log(quoteText)
Docs are here. Browsers that support template literals listed here.
Upvotes: 18
Reputation: 334
This can be one of several solutions:
var text = "http://example.com";
JSON.stringify(text).replace('\"', '\"\'').replace(/.$/, '\'"')
Upvotes: -2
Reputation: 322
I think, the best and easy way for you, to put value inside quotes is:
JSON.stringify(variable or value)
Upvotes: 17
Reputation: 31
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+"'";
Upvotes: 3
Reputation: 4492
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:
Example
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
Example
var answer='It\'s alright';
var answer="He is called \"Johnny\"";
http://www.w3schools.com/js/js_obj_string.asp
Upvotes: 8
Reputation: 2548
let's think urls = "http://example1.com http://example2.com"
function somefunction(urls){
var urlarray = urls.split(" ");
var text = "\"'" + urlarray[0] + "'\"";
}
output will be text = "'http://example1.com'"
Upvotes: 3
Reputation: 2795
var text = "\"http://www.example1.com\"; \"http://www.example2.com\"";
Using escape sequence of " (quote), you can achieve this
You can place singe quote (') inside double quotes without any issues Like this
var text = "'http://www.ex.com';'http://www.ex2.com'"
Upvotes: 1
Reputation: 68152
Lets assume you have a bunch of urls separated by spaces. In this case, you could do this:
function quote(text) {
var urls = text.split(/ /)
for (var i = 0; i < urls.length; i++) urls[i] = "'" + urls[i] + "'"
return urls.join(" ")
}
This function takes a string like "http://example.com http://blarg.test"
and returns a string like "'http://example.com' 'http://blarg.test'"
.
It works very simply: it takes your string of urls, splits it by spaces, surrounds each resulting url with quotes and finally combines all of them back with spaces.
Upvotes: 1
Reputation: 3490
var text = "http://example.com";
text = "'"+text+"'";
Would attach the single quotes (') to the front and the back of the string.
Upvotes: 34
Reputation: 382696
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"
Upvotes: 59