我在Ruby中有以下代码。我想把这段代码转换成JavaScript。JS中的等效代码是什么?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
我在Ruby中有以下代码。我想把这段代码转换成JavaScript。JS中的等效代码是什么?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
当前回答
您可以使用TypeScript(JavaScript SuperSet),它支持多行字符串,并将文件向下转换为纯JavaScript而无需开销:
var templates = {
myString: `this is
a multiline
string`
}
alert(templates.myString);
如果您想用普通JavaScript实现同样的功能:
var templates =
{
myString: function(){/*
This is some
awesome multi-lined
string using a comment
inside a function
returned as a string.
Enjoy the jimmy rigged code.
*/}.toString().slice(14,-3)
}
alert(templates.myString)
请注意,iPad/Safari不支持“functionName.toString()”
如果您有很多遗留代码,也可以在TypeScript中使用纯JavaScript变量(用于清理):
interface externTemplates
{
myString:string;
}
declare var templates:externTemplates;
alert(templates.myString)
您可以使用普通JavaScript变体中的多行字符串对象,将模板放入另一个文件中(可以合并到捆绑包中)。
您可以在以下位置尝试TypeScripthttp://www.typescriptlang.org/Playground
其他回答
在这里找到了很多过度设计的答案。我认为最好的两个答案是:
1:
let str = `Multiline string.
foo.
bar.`
其最终记录:
Multiline string.
foo.
bar.
2:
let str = `Multiline string.
foo.
bar.`
这会正确地记录它,但如果str嵌套在函数/对象等内部,则脚本文件中会很难看。:
Multiline string.
foo.
bar.
我非常简单的回答是用正则表达式正确记录str:
let str = `Multiline string.
foo.
bar.`.replace(/\n +/g, '\n');
请注意,这不是一个完美的解决方案,但如果您确定在新行(\n)之后至少会出现一个空格(+表示至少出现一次),则可以使用。它还可以与*(零或更多)一起使用。
您可以更明确地使用{n,},这意味着至少出现n次。
您可以使用标记的模板来确保获得所需的输出。
例如:
// Merging multiple whitespaces and trimming the output
const t = (strings) => { return strings.map((s) => s.replace(/\s+/g, ' ')).join("").trim() }
console.log(t`
This
Is
A
Multiline
String
`);
// Output: 'This Is A Multiline String'
// Similar but keeping whitespaces:
const tW = (strings) => { return strings.map((s) => s.replace(/\s+/g, '\n')).join("").trim() }
console.log(tW`
This
Is
A
Multiline
String
`);
// Output: 'This\nIs\nA\nMultiline\nString'
刚刚尝试了Anonymous的答案,发现这里有一个小技巧,如果反斜杠后面有空格,它就不起作用了因此,以下解决方案不起作用-
var x = { test:'<?xml version="1.0"?>\ <-- One space here
<?mso-application progid="Excel.Sheet"?>'
};
但当空间被移除时,它会起作用-
var x = { test:'<?xml version="1.0"?>\<-- No space here now
<?mso-application progid="Excel.Sheet"?>'
};
alert(x.test);
希望它有帮助!!
在JavaScript中打印多行字符串的一种简单方法是使用由反引号(``)表示的模板文本(模板字符串)。还可以在模板字符串中使用变量,如(“name is${value}”)
你也可以
常量值=`multiline`const text=`这是一个${value}js中的字符串`;console.log(文本);
我这样编程:
sys = {
layout: null,
makeLayout: function (obCLS) {
this.layout = $('<div />').addClass('editor').appendTo($(obCLS)).append(
/* Cargador */
/* @this.layout.find('> div:nth-of-child(1)'); */
'<div>' +
' <p>Seleccione la imagen que desea procesar.</p>' +
' <input type="button" value="Seleccionar" class="btn btn-xlarge btn-success" />' +
' <span></span>' +
'</div>' +
/* Cargador - Progreso */
/* @this.layout.find('> div:nth-of-child(2)'); */
'<div>' +
' <div>' +
' <div></div>' +
' <div>' +
' <div></div>' +
' </div>' +
' </div>' +
'</div>' +
/* Editor */
/* @this.layout.find('> div:nth-of-child(3)');
* @sidebar = this.layout.find('> div:nth-of-child(3) > div > div > div:nth-of-type(1)');
* @body = this.layout.find('> div:nth-of-child(3) > div > div > div:nth-of-type(2) > div'); */
'<div>' +
' <div>' +
' <div>' +
' <div></div>' +
' <div>' +
' <div></div>' +
' </div>' +
' </div>' +
' </div>' +
'</div>'
);
}
}
sys.makeLayout('#div');