"Go" embedding a large number of string constants in a JavaScript file is something that is often encountered. Sometimes for the sake of convenience, some interface HTML and CSS directly written in the JS file. Small number of good, more words on a lump of text, pay attention to the artistic youth, will use a lot of character connection symbols even with indentation, forced into several lines. For example:
var html = ' < div > ' + ' < p > hello</ p > ' + ' < > World ' </ p > "+ ' </ div > ';
This is OK, if there are a lot of double quotation marks in the string, it is more troublesome, a variety of escape characters to see the dazzling.
In fact, there is a little small skill, can solve the problem. People always think that the string must be in the "..." or "...", that's right. But there is also a place where the string can also be preserved as non-code semantics, which is a function of the ToString, the entire function of the code when the string output-the annotation part of course is also reserved!
So we write an empty function, which is a/**/comment, which is the constant content we want. After ToString, add a regular to extract what we want!
Try it now:
varRes_code = _text (function(){/*#include <iostream> int main () {std::cout << "Hello World" << Std::endl; return 0; }*/});varRes_poem = _text (function(){/*more blows, the stars as rain. BMW Carving Car Incense full road. Feng Xiao Sound movement, jade pot light turn, one night fish dragon dance. Moth son snow willow golden wisp, laughter and dark incense go. The crowd found him 1100 degrees, suddenly look back, the man is in the dim place. */});varRes_xml = _text (function(){/*<projectDescription> <name>Hello</name> <comment></comment> < ;p rojects> </projects> <buildSpec> <buildCommand> <name> ;com.adobe.flexbuilder.project.flexbuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.adobe.flexbuilder.project .apollobuilder</name> <arguments> </arguments> </buildcomman d> </buildSpec> </projectDescription>*/});function_text (Wrap) {returnWrap.tostring (). Match (/\/\*\s ([\s\s]*) \s\*\//) [1];} alert (res_code); alert (res_poem); alert (res_xml) ;
Methods for embedding a large number of string constants in JavaScript