你可以在不使用'或'的情况下创建JavaScript字符串吗?报价? [英] Can you create a JavaScript string without using ' or " quotes?

查看:77
本文介绍了你可以在不使用'或'的情况下创建JavaScript字符串吗?报价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些XML的JS文件,其中XML应该由服务器转换为单词。

I have a JS file with some XML in it, where the XML is supposed to get converted to a word by the server.

例如

var ip = "<lang:cond><lang:when test="$(VAR{'ip_addr'})">$(VAR{'ip_addr'})</lang:when></lang:cond>";

这会转换为:

var ip = "192.168.0.0";

但是,如果服务器无法按预期工作,我不希望有语法错误,这非常重要。目前会出现语法错误,因为该语言使用两种类型的引号。我想不出办法解决这个问题,但也许还有另一种方法可以在JavaScript中做引用?或者创建一个字符串?

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?

例如,在Python中我会使用三重引号:

For example, in Python I'd use triple quotes:

ip = """<lang:cond><lang:when test="$(VAR{'ip_addr'})">$(VAR{'ip_addr'})</lang:when></lang:cond>"""

任何人都有一个好主意?

Anyone have a bright idea?

推荐答案

我不得不为项目创建没有引号的字符串。我们正在向浏览器提供可执行的客户端javascript以用于内部网站。接收端在显示时会删除双引号和单引号。我发现绕过引号的一种方法是将我的字符串声明为正则表达式。

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.

var x = String(/This contains no quotes/);
x = x.substring(1, x.length-1);
x;

这篇关于你可以在不使用'或'的情况下创建JavaScript字符串吗?报价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆