[英]Put quotes around a variable string in JavaScript
I have a JavaScript variable:我有一个 JavaScript 变量:
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)
尝试:
var text = "'" + "http://example.com" + "'";
To represent the text below in JavaScript:用 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/ JS 小提琴: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 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 "让我们想想 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'"输出将是 text = "'http://example1.com'"
You can escape " with \你可以用 \ 转义 "
var text="\"word\"";
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'"
.这个函数接受一个像
"http://example.com http://blarg.test"
这样的字符串,并返回一个像"'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.它的工作原理非常简单:它获取您的 url 字符串,用空格分割,用引号包围每个生成的 url,最后用空格将它们全部组合起来。
Another easy way to wrap a string is to extend the Javascript String prototype:另一种包装字符串的简单方法是扩展 Javascript String 原型:
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(/.$/, '\'"')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.