40

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'"
slayernoah
  • 4,382
  • 11
  • 42
  • 73
re1man
  • 2,337
  • 11
  • 38
  • 54

13 Answers13

59
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"
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
34
var text = "http://example.com";

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

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

Ajai
  • 3,440
  • 5
  • 28
  • 41
17

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.

Dispenser
  • 1,111
  • 7
  • 9
mplno
  • 187
  • 1
  • 5
14

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

JSON.stringify(variable or value)
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • Note that this will also backslash-escape things like newlines and tabs, which may or may not be what you want. – Tamlyn Mar 10 '22 at 09:54
9

Try:

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

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
7

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

slayernoah
  • 4,382
  • 11
  • 42
  • 73
3

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+"'";

TResponse
  • 3,940
  • 7
  • 43
  • 63
anshul
  • 31
  • 2
3

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'"

Darshana
  • 2,462
  • 6
  • 28
  • 54
2

You can escape " with \

var text="\"word\"";

http://jsfiddle.net/beKpE/

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
1

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"
edj
  • 523
  • 7
  • 17
1
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'"
Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
1

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.

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
-2

This can be one of several solutions:

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

JSON.stringify(text).replace('\"', '\"\'').replace(/.$/, '\'"')
Akbar Taghipour
  • 334
  • 6
  • 23
  • 5
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Oct 03 '20 at 10:08