Javascript中拼接大量字符串的方法_javascript技巧
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 21:34:07
Javascript中拼接大量字符串的方法_javascript技巧
Javascript中拼接大量字符串的方法_javascript技巧:在php、python中都有heredoc方式的字符串定義方法: php: 代碼如下: $sql= select * from pages where pagename='$pn' EOD; python: 代碼如下: print This is an example of a string in the heredo
導(dǎo)讀Javascript中拼接大量字符串的方法_javascript技巧:在php、python中都有heredoc方式的字符串定義方法: php: 代碼如下: $sql= select * from pages where pagename='$pn' EOD; python: 代碼如下: print This is an example of a string in the heredo
在php、python中都有heredoc方式的字符串定義方法:
php:
代碼如下:
$sql=<<
select *
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拼接大量字符串沒(méi)個(gè)heredoc風(fēng)格的操作符是比較繁瑣的:
拼接方式一:
代碼如下:
var str = "\
Here is line one \
And line two \
Finally, line three! \
";
alert(str);
拼接方式二:
代碼如下:
var __template =
'
'+
'#salarySN# | '+
'#name# | '+
'#TDR_NAME# | '+
'#TSD_NAME# | '+
'#WORK_STATUS# | '+
'#isleader_display# | '+
''
+'設(shè)置角色'
+' |
';
JS字符串需要打破原字符串風(fēng)格,每行處理,這點(diǎn)有點(diǎn)讓人受不了。
給個(gè)解決方案:
代碼如下:
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)去掉首尾兩行函數(shù)定義的代碼,重新組裝即可。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
Javascript中拼接大量字符串的方法_javascript技巧
Javascript中拼接大量字符串的方法_javascript技巧:在php、python中都有heredoc方式的字符串定義方法: php: 代碼如下: $sql= select * from pages where pagename='$pn' EOD; python: 代碼如下: print This is an example of a string in the heredo