我有以下的 Ruby 代码。我想将这段代码转换成 JavaScript。请问在 JavaScript 中等价的代码是什么?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
我有以下的 Ruby 代码。我想将这段代码转换成 JavaScript。请问在 JavaScript 中等价的代码是什么?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
正如第一个回答提到的那样,使用ES6/Babel,您现在可以通过使用反引号来创建多行字符串:
const htmlString = `Say hello to
multi-line
strings!`;
插值变量是使用反引号定界字符串的一种流行新功能:
const htmlString = `${user.name} liked your post about strings`;
这只是转换为连接:
user.name + ' liked your post about strings'
Google's JavaScript style guide recommends to use string concatenation instead of escaping newlines:
Do not do this:
var myString = 'A rather long string of English text, an error message \ actually that just keeps going and going -- an error \ message to make the Energizer bunny blush (right through \ those Schwarzenegger shades)! Where was I? Oh yes, \ you\'ve got an error and all the extraneous whitespace is \ just gravy. Have a nice day.';
The whitespace at the beginning of each line can't be safely stripped at compile time; whitespace after the slash will result in tricky errors; and while most script engines support this, it is not part of ECMAScript.
Use string concatenation instead:
var myString = 'A rather long string of English text, an error message ' + 'actually that just keeps going and going -- an error ' + 'message to make the Energizer bunny blush (right through ' + 'those Schwarzenegger shades)! Where was I? Oh yes, ' + 'you\'ve got an error and all the extraneous whitespace is ' + 'just gravy. Have a nice day.';
\
而不是\\
结束一行,则很难注意到],以及(3)虽然大多数脚本引擎都支持此功能,但它不是ECMAScript的一部分[即为什么使用非标准功能?]请记住,这是一个样式指南,其目的是使代码易于阅读、维护和调试,而不仅仅是“能运行”。 - ShreevatsaR在js中并没有可用的模式text = <<"HERE" This Is A Multiline String HERE
(我记得在我以前使用Perl的旧日子里它经常被使用)。
为了更好地处理复杂或长的多行字符串,我有时会使用数组模式:
var myString =
['<div id="someId">',
'some content<br />',
'<a href="#someRef">someRefTxt</a>',
'</div>'
].join('\n');
或者像anonymous展示的模式一样(转义换行符),这可能会成为代码中一个丑陋的块:
var myString =
'<div id="someId"> \
some content<br /> \
<a href="#someRef">someRefTxt</a> \
</div>';
这里有另一个奇怪但可行的“技巧”1:
var myString = (function () {/*
<div id="someId">
some content<br />
<a href="#someRef">someRefTxt</a>
</div>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
外部编辑:jsfiddle
ES20xx支持使用模板字符串跨多行拼接字符串:
let str = `This is a text
with multiple lines.
Escapes are interpreted,
\n is a newline.`;
let str = String.raw`This is a text
with multiple lines.
Escapes are not interpreted,
\n is not a newline.`;
1 注意:这将在压缩/混淆代码后丢失
在纯JavaScript中,您可以使用多行字符串。
该方法基于函数的序列化,这被定义为依赖于实现的。它在大多数浏览器中都能工作(请参阅下文),但不能保证它将来仍将起作用,所以不要依赖它。
使用以下函数:
function hereDoc(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '');
}
您可以像这样使用here-documents:
您可以像这样使用here-documents:
var tennysonQuote = hereDoc(function() {/*!
Theirs not to make reply,
Theirs not to reason why,
Theirs but to do and die
*/});
该方法已在以下浏览器中成功测试(未提及 = 未测试):
但要小心你的代码压缩器。它往往会删除注释。对于YUI 压缩器,以 /*!
开头的注释(就像我用的那个)将被保留。
我认为一个真正的解决方案是使用CoffeeScript。
ES6 更新:您可以使用反引号代替创建带有注释并运行 toString 的函数。正则表达式需要更新以仅剥离空格。您还可以拥有一个字符串原型方法来完成此操作:
let foo = `
bar loves cake
baz loves beer
beer loves people
`.removeIndentation()
应该有人写一个 .removeIndentation 字符串方法... ;)
你可以这样做...
var string = 'This is\n' +
'a multiline\n' +
'string';
我想出了一种非常简单的多行字符串的方法。由于将函数转换为字符串还会返回函数内部的任何注释,因此您可以使用多行注释作为字符串。您只需要修剪两端并得到您的字符串。
var 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(myString)
toString()
中的空格有多精确。) - Kevin Cox我很惊讶我没有看到这个,因为它在我测试的所有地方都有效,并且对于例如模板非常有用:
<script type="bogus" id="multi">
My
multiline
string
</script>
<script>
alert($('#multi').html());
</script>
有没有人知道一个环境,其中包含HTML,但它不起作用?
<div id="UniqueID" style="display:none;">
Strings
On
Multiple
Lines
Here
</div>
那么当我需要获取该字符串时,只需使用以下jQuery:
$('#UniqueID').html();
这种方法可以让我的文本跨越多行显示。如果我调用
alert($('#UniqueID').html());
我理解您需要进行翻译的内容如下:
我得到的结果是:
display:none
的内容,这很可能是由于JavaScript样式的前端变得越来越流行。(例如,一个带有隐藏/显示功能的FAQ页面。)但你需要小心,因为Google表示,如果隐藏的内容似乎是为了人为地提高你的SEO排名而设计的,他们可以惩罚你。 - Gavin有多种方法可以实现这一点。
1. 斜杠连接
var MultiLine= '1\
2\
3\
4\
5\
6\
7\
8\
9';
2. 普通连接
var MultiLine = '1'
+'2'
+'3'
+'4'
+'5';
3. 数组连接
var MultiLine = [
'1',
'2',
'3',
'4',
'5'
].join('');
就性能而言,斜杠连接(第一个)是最快的。
有关性能的更多详细信息,请参见此测试案例
更新:
使用ES2015版本,我们可以利用其模板字符串功能。使用它,我们只需使用反引号创建多行字符串。
例如:
`<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div><label>name: </label>{{hero.name}}</div>
`
使用脚本标签:
<script>...</script>
代码块添加到 head
标签中;获取您的多行文本原样...(注意文本编码:UTF-8、ASCII)
<script>
// pure javascript
var text = document.getElementById("mySoapMessage").innerHTML ;
// using JQuery's document ready for safety
$(document).ready(function() {
var text = $("#mySoapMessage").html();
});
</script>
<script id="mySoapMessage" type="text/plain"></script>
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
。我记得除了在JS中拼写错误的注释之外,没有遇到其他问题。空格不是问题。 - jpfreire