Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何编写没有转义字符的长字符串文字?_Javascript - Fatal编程技术网

Javascript 如何编写没有转义字符的长字符串文字?

Javascript 如何编写没有转义字符的长字符串文字?,javascript,Javascript,我有一个脚本(用另一种语言)生成有效的JavaScript片段,然后在浏览器中执行。生成的javascript如下所示: my_function(123,"long string with lots of weird characters"); “长字符串”可能包含引号、撇号、反斜杠等。。。例如,“长字符串”可以是以下任意一种: hello"there hello'there hello\\\\"\\\'\'\\\'"'\"\\&qu

我有一个脚本(用另一种语言)生成有效的JavaScript片段,然后在浏览器中执行。生成的javascript如下所示:

my_function(123,"long string with lots of weird characters");
“长字符串”可能包含引号、撇号、反斜杠等。。。例如,“长字符串”可以是以下任意一种:

hello"there
hello'there
hello\\\\"\\\'\'\\\'"'\"\\"""there
所有这些字符都应按原样传递给my_function(),而不假定反斜杠是转义的特殊字符

javascript是否有某种独特的“标记”来分隔长字符串文字,其中不应“转义”或“解释”任何内容?例如,与此类似的构造:

my_function(123, [<STRING_START>]long string with lots of weird characters[<STRING_END>]);
my_函数(123,[]长字符串,包含许多奇怪的字符[]);
我需要这样的东西,并且我可以保证我的长字符串不会包含字符串“][]”,这样就可以了。然而,我不能轻易保证它不会包含引号和/或反斜杠


我知道我可以使用普通引号来分隔字符串,并在字符串中的所有必需字符之前添加反斜杠(在我的javascript生成器中),但是上面显示的“标记”的存在(或类似的东西)会让我的生活更轻松。

您可以使用字符串。raw

它用于获取模板文本的原始字符串形式,即, 替换(例如${foo})被处理,但转义(例如.\n)被处理 不是


嗯,任何用来表示字符串结尾的字符都需要在字符串中转义。有一个
String.raw
标记的模板(
String.raw`\试试这个`
),但是您仍然需要在值本身内部转义反勾号。这就是为什么我要询问多字节开始/结束标记。我可以很容易地保证它不会出现在我的字符串中。那是不存在的。唯一的“字符串”分隔符是
`
var hello= String.raw` there
hello'there
hello\\\\"\\\'\'\\\'"'\"\\"""there `;