你能在不使用 ' 或 " 引号的情况下创建 JavaScript 字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8981009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you create a JavaScript string without using ' or " quotes?
提问by Jeff
I have a JS file with some XML in it, where the XML is supposed to get converted to a word by the server.
我有一个包含一些 XML 的 JS 文件,其中 XML 应该被服务器转换为一个单词。
E.g.
例如
var ip = "<lang:cond><lang:when test="$(VAR{'ip_addr'})">$(VAR{'ip_addr'})</lang:when></lang:cond>";
This gets converted to:
这将转换为:
var ip = "192.168.0.0";
However, in case the server doesn't work as intended, I don't want there to be a syntax error, and this is VERY important. Currently there would be a syntax error because the language uses both types of quotes. I can't think of a way to get around this, but perhaps there's another way to do quotes in JavaScript? Or to create a string?
但是,如果服务器无法按预期工作,我不希望出现语法错误,这非常重要。当前会出现语法错误,因为该语言使用两种类型的引号。我想不出解决这个问题的方法,但也许还有另一种方法可以在 JavaScript 中引用?或者创建一个字符串?
For example, in Python I'd use triple quotes:
例如,在 Python 中我会使用三引号:
ip = """<lang:cond><lang:when test="$(VAR{'ip_addr'})">$(VAR{'ip_addr'})</lang:when></lang:cond>"""
Anyone have a bright idea?
有人有一个好主意吗?
回答by Josh
I have had to create strings without quotes for a project as well. We were delivering executable client javascript to the browser for an internal website. The receiving end strips double and single quotes when displayed. One way I have found to get around quotes is by declaring my string as a regular expression.
我还必须为项目创建没有引号的字符串。我们正在向内部网站的浏览器提供可执行的客户端 javascript。接收端显示时去掉双引号和单引号。我发现绕过引号的一种方法是将我的字符串声明为正则表达式。
var x = String(/This contains no quotes/);
x = x.substring(1, x.length-1);
x;
回答by PiTheNumber
Using Stringprototype:
使用字符串原型:
String(/This contains no quotes/).substring(1).slice(0,-1)
Using String.fromCharCode
使用String.fromCharCode
String.fromCharCode(72,69,76,76,79)
Generate Char Codes for this:
为此生成字符代码:
var s = "This contains no quotes";
var result = [];
for (i=0; i<s.length; i++)
{
result.push(s.charCodeAt(i));
}
result
回答by www139
I was looking for a solution to the same problem. Someone suggested looking at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_stringswhich proved helpful. After reading about half the article, it stated that you can create strings with the backward tick character. (`)
我正在寻找相同问题的解决方案。有人建议查看https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings,这证明很有帮助。在阅读了大约一半的文章后,它说您可以使用向后的刻度字符创建字符串。(`)
Try this :)
尝试这个 :)
document.getElementById('test').innerHTML = `'|'|'|"|"`
<div id="test" style="font-size:3em;"></div>
回答by ziesemer
In JavaScript, you can escape either type of quote with a \
.
在 JavaScript 中,您可以使用\
.
For example:
例如:
var str = "This is a string with \"embedded\" quotes.";
var str2 = 'This is a string with \'embedded\' quotes.';
In particular, your block of JavaScript code should be converted to:
特别是,您的 JavaScript 代码块应转换为:
var ip = "<lang:cond><lang:when test=\"$(VAR{'ip_addr'})\">$(VAR{'ip_addr'})</lang:when></lang:cond>";
In general, I always prefer to escape the quotes instead of having to constantly switch quote types, depending upon what type of quotes may be used within.
一般来说,我总是喜欢对引号进行转义,而不是不断地切换引号类型,这取决于其中可能使用的引号类型。
回答by buley
You can't create a string without using a single or double quote, as even calling the String()
prototype object directly still requires you to pass it the string.
不使用单引号或双引号就不能创建字符串,因为即使String()
直接调用原型对象仍然需要您将字符串传递给它。
Inside XML you would use CDATA, but inside JS you'll have to just escape the '\"strings\"'
"\'appropriately\'"
在 XML 中,您将使用 CDATA,但在 JS 中,您必须转义 '\"strings\"'
"\'appropriately\'"