This article mainly introduces how to splice a large number of strings in Javascript. This article implements the Heredoc syntax in JS language, for more information about how to define strings in heredoc mode in php and python, see:
Php:
The Code is as follows:
$ SQL = < Select *
From pages
Where pagename = '$ pn'
EOD;
Python:
The Code is as follows:
Print """
This is an example of a string in the heredoc syntax.
This text can span multiple lines
"""
Js concatenates a large number of strings without heredoc-style operators, which is complicated:
Splicing Method 1:
The Code is as follows:
Var str = "\
Here is line one \
And line two \
Finally, line three! \
";
Alert (str );
Splicing Method 2:
The Code is as follows:
Var _ template =
''+
'# SalarySN #'+
'# Name #'+
'# TDR_NAME #'+
'# TSD_NAME #'+
'# WORK_STATUS #'+
'# Isleader_display #'+
''
+ 'Set role'
+'';
JS Strings need to break the original string style, and each line is processed, which is a bit unbearable.
Provide a solution:
The Code is as follows:
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 ));
Use func. toString () to obtain the string to be processed in batches. Use split (/\ n /). slice (1,-1) removes the first and last line of function-defined code and reassembles it.