Ver*_*Lom 104 javascript heredoc
我在JavaScript中需要类似heredoc的东西.你有什么想法吗?我需要跨浏览器功能.
我找到了这个:
heredoc = '\
<div>\
<ul>\
<li><a href="#zzz">zzz</a></li>\
</ul>\
</div>';
Run Code Online (Sandbox Code Playgroud)
我认为这对我有用.:)
mko*_*mko 67
尝试ES6字符串模板,你可以做类似的事情
var hereDoc = `
This
is
a
Multiple
Line
String
`.trim()
hereDoc == 'This\nis\na\nMultiple\nLine\nString'
=> true
Run Code Online (Sandbox Code Playgroud)
今天你可以使用6to5或TypeScript来使用这个强大的功能
And*_*are 61
不,不幸的是JavaScript不支持像heredoc这样的东西.
Zv_*_*oDD 37
这个怎么样:
function MyHereDoc(){
/*HERE
<div>
<p>
This is written in the HEREDOC, notice the multilines :D.
</p>
<p>
HERE
</p>
<p>
And Here
</p>
</div>
HERE*/
var here = "HERE";
var reobj = new RegExp("/\\*"+here+"\\n[\\s\\S]*?\\n"+here+"\\*/", "m");
str = reobj.exec(MyHereDoc).toString();
str = str.replace(new RegExp("/\\*"+here+"\\n",'m'),'').toString();
return str.replace(new RegExp("\\n"+here+"\\*/",'m'),'').toString();
}
//Usage
document.write(MyHereDoc());
Run Code Online (Sandbox Code Playgroud)
只需用选择的单词替换"/*HERE"和"HERE*/"即可.
Nat*_*ero 33
基于Zv_oDD的答案,我创建了一个类似的函数,以便于重用.
警告:这是许多JS解释器的非标准功能,可能会在某些时候被删除,但由于我正在构建一个仅用于Chrome的脚本,我正在使用它!不要永远依靠这对于面向客户的网站!
// Multiline Function String - Nate Ferrero - Public Domain
function heredoc(fn) {
return fn.toString().match(/\/\*\s*([\s\S]*?)\s*\*\//m)[1];
};
Run Code Online (Sandbox Code Playgroud)
使用:
var txt = heredoc(function () {/*
A test of horrible
Multi-line strings!
*/});
Run Code Online (Sandbox Code Playgroud)
返回:
"A test of horrible
Multi-line strings!"
Run Code Online (Sandbox Code Playgroud)
笔记:
编辑:
2014年2月2日 - 改为完全没有使用函数原型,而是使用名称heredoc.
5/26/2017 - 更新空白以反映现代编码标准.
Dav*_*art 19
根据您正在运行的JS/JS引擎的风格(SpiderMonkey,AS3),您可以简单地编写内联XML,您可以将文本放在多行上,例如heredoc:
var xml = <xml>
Here
is
some
multiline
text!
</xml>
console.log(xml.toXMLString())
console.log(xml.toString()) // just gets the content
Run Code Online (Sandbox Code Playgroud)
Cha*_*lie 13
ES6 模板字符串具有heredoc功能.
您可以声明由back-tick(``)括起来的字符串,并且可以通过多行扩展.
var str = `This is my template string...
and is working across lines`;
Run Code Online (Sandbox Code Playgroud)
您还可以在模板字符串中包含表达式.这些由美元符号和花括号(${expression}
)表示.
var js = "Java Script";
var des = `Template strings can now be used in ${js} with lot of additional features`;
console.log(des); //"Template strings can now be used in Java Script with lot of additional features"
Run Code Online (Sandbox Code Playgroud)
实际上有更多的功能,如Tagged Temple Strings和Raw Strings.请在下面找到文档
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
您可以使用CoffeeScript,这是一种可编译为JavaScript的语言.代码一对一地编译成等效的JS,并且在运行时没有解释.
当然,它有heredocs :)
我只是为@NateFerrero的答案扩展而单独写答案我感觉很糟糕,但我不觉得编辑他的答案也是合适的,所以如果这个答案对你有用,请提请@NateFerrero.
我主要需要Javascript heredoc来存储一块CSS,例如
var css = heredoc(function() {/*
/**
* Nuke rounded corners.
*/
body div {
border-top-left-radius: 0 !important;
border-top-right-radius: 0 !important;
border-bottom-right-radius: 0 !important;
border-bottom-left-radius: 0 !important;
}
*/});
Run Code Online (Sandbox Code Playgroud)
然而,正如你所看到的,我喜欢评论我的CSS,不幸的是(正如语法突出显示)第一个*/
结束整体评论,打破了heredoc.
对于这个特定目的(CSS),我的解决方法是添加
.replace(/(\/\*[\s\S]*?\*) \//g, '$1/')
Run Code Online (Sandbox Code Playgroud)
到@NateFerrero里面的连锁店heredoc
; 完整形式:
function heredoc (f) {
return f.toString().match(/\/\*\s*([\s\S]*?)\s*\*\//m)[1].replace(/(\/\*[\s\S]*?\*) \//g, '$1/');
};
Run Code Online (Sandbox Code Playgroud)
和由之间添加空间使用它*
并/
为"内部"的块注释,像这样:
var css = heredoc(function() {/*
/**
* Nuke rounded corners.
* /
body div {
border-top-left-radius: 0 !important;
border-top-right-radius: 0 !important;
border-bottom-right-radius: 0 !important;
border-bottom-left-radius: 0 !important;
}
*/});
Run Code Online (Sandbox Code Playgroud)
在replace
简单地发现/* ... * /
并消除空间使/* ... */
,从而保持定界符直到调用.
您当然可以完全删除评论
.replace(/\/\*[\s\S]*?\* \//g, '')
Run Code Online (Sandbox Code Playgroud)
//
如果将注释添加到链中,您还可以支持注释:
.replace(/^\s*\/\/.*$/mg, '')
Run Code Online (Sandbox Code Playgroud)
此外,你可以做除了*
和之间的单一空间以外的事情/
,如-
:
/**
* Nuke rounded corners.
*-/
Run Code Online (Sandbox Code Playgroud)
如果你只是适当地更新正则表达式:
.replace(/(\/\*[\s\S]*?\*)-\//g, '$1/')
^
Run Code Online (Sandbox Code Playgroud)
或者你可能想要一个任意数量的空白而不是一个空格?
.replace(/(\/\*[\s\S]*?\*)\s+\//g, '$1/')
^^^
Run Code Online (Sandbox Code Playgroud)
小智 7
ES5和更早版本
Run Code Online (Sandbox Code Playgroud)(function(){/** some random multi line text here **/}).toString().slice(15,-5);
ES6及更高版本
Run Code Online (Sandbox Code Playgroud)`some random multi line text here`
结果
Run Code Online (Sandbox Code Playgroud)some random multi line text here
正如其他人所说,ES6 模板字符串为您提供了传统heredocs 提供的大部分内容。
如果您想更进一步并使用带标记的模板字符串,theredoc
这是一个很好的实用程序函数,可以让您执行此操作:
if (yourCodeIsIndented) {
console.log(theredoc`
Theredoc will strip the
same amount of indentation
from each line.
You can still indent
further if you want.
It will also chop off the
whitespace-only first and
last lines.
`)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
52692 次 |
最近记录: |