How to embed a large number of string constants in JavaScript

Source: Internet
Author: User

If the number is small, it's okay. If the number is large, there will be a piece of text. The beautiful young people will use a lot of character connection symbols or even add indentation, which will be forced to be replaced by several lines. For example:
Copy codeThe Code is as follows:
Var html =
'<Div>' +
'<P> Hello </p>' +
'<P> world' </p>' +
'</Div> ';

This is fine. If there are many double quotation marks in the string, it will be more troublesome and the various escape characters will be dazzled.

In fact, you can solve this problem if you have little tips. We all think that the string must be in "..." or '...'. That's right. However, there is another place where strings can also be saved as non-code semantics, that is, the toString of a function, and the code of the entire function is output as a string-the comments are also retained!

Therefore, we write an empty function with a/**/comment in it, which is the constant content we want. Add a regular expression after toString to extract what we want!

Try now:

Copy codeThe Code is as follows:
Var RES_CODE = _ TEXT (function (){/*
# Include <iostream>

Int main ()
{
Std: cout <"Hello world" <std: endl;
Return 0;
}
*/});

Var RES_POEM = _ TEXT (function (){/*
Even more, stars are like rain.
Xiangmanlu, a BMW car.
Fengyi sound, the jade pot light turn, a night of fish dragon dance.
Moth snow Willow yellow wisp,
The smile is everywhere.
Search for him in the crowd,
Looking back, the man was in the dark.
*/});

Var RES_XML = _ TEXT (function (){/*
<ProjectDescription>
<Name> Hello </name>
<Comment> </comment>
<Projects>
</Projects>
<BuildSpec>
<BuildCommand>
<Name> com. adobe. flexbuilder. project. flexbuilder </name>
<Arguments>
</Arguments>
</BuildCommand>
<BuildCommand>
<Name> com. adobe. flexbuilder. project. Apollo builder </name>
<Arguments>
</Arguments>
</BuildCommand>
</BuildSpec>
</ProjectDescription>
*/});

Function _ TEXT (wrap ){
Return wrap. toString (). match (// \/\ * \ s ([\ s \ S] *) \ s \ * \/) [1];
}
Alert (RES_CODE );
Alert (RES_POEM );
Alert (RES_XML );

OK! In Chrome, the tabs in front of each line of comment are removed. If you just put the code, the problem is not very serious ~ Of course, there is a premise that the character cannot appear */

It is worth noting that comments are filtered out when the code is compressed, and some comments need to be excluded manually.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.