php:
代码如下:
$sql=<<
from pages
where pagename='$pn'
EOD;
python:
代码如下:
print """
This is an example of a string in the heredoc syntax.
This text can span multiple lines
"""
js拼接大量字符串没个heredoc风格的操作符是比较繁琐的:
拼接方式一:
代码如下:
var str = "\
Here is line one \
And line two \
Finally, line three! \
";
alert(str);
拼接方式二:
代码如下:
var __template =
'
给个解决方案:
代码如下:
function aHereDoc() {/*
Hello, World!
I am a JavaScript here document.
Use the 'hereDoc' function to extract me.
*/}
function hereDoc(func) {
return func.toString().split(/\n/).slice(1, -1).join('\n');
}
console.log(hereDoc(aHereDoc));
利用func.toString()获取需要批量处理的字符串,利用split(/\n/).slice(1, -1)去掉首尾两行函数定义的代码,重新组装即可。