re1man
re1man

Reputation: 2367

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:

"'http://example.com'"

Upvotes: 40

Views: 236339

Answers (13)

edj
edj

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

mplno
mplno

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

Akbar Taghipour
Akbar Taghipour

Reputation: 334

This can be one of several solutions:

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

JSON.stringify(text).replace('\"', '\"\'').replace(/.$/, '\'"')

Upvotes: -2

Syed Shahzaib Hussain
Syed Shahzaib Hussain

Reputation: 322

I think, the best and easy way for you, to put value inside quotes is:

JSON.stringify(variable or value)

Upvotes: 17

anshul
anshul

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

slayernoah
slayernoah

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:

  • 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

Upvotes: 8

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try:

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

Upvotes: 9

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8530

You can escape " with \

var text="\"word\"";

http://jsfiddle.net/beKpE/

Upvotes: 2

Darshana
Darshana

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

Sunil Kumar B M
Sunil Kumar B M

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

Tikhon Jelvis
Tikhon Jelvis

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

Ajai
Ajai

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

Sarfraz
Sarfraz

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

Related Questions