简体   繁体   中英

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'"
var text = "\"http://example.com\""; 

无论你的文字是什么,要用"包装它,你需要把它们放在里面并用\转义。上面将导致:

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

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

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

我认为,对您来说,将价值放在引号内的最好和最简单的方法是:

JSON.stringify(variable or value)

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 .

尝试:

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

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

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

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

You can escape " with \

var text="\"word\"";

http://jsfiddle.net/beKpE/

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

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.

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"

This can be one of several solutions:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM