[Regular expression] greedy mode and non-Greedy Mode

Source: Internet
Author: User

Copy codeThe Code is as follows:
/**
** Author: site120
** Function: get script part from html document
**/
Var loadJs = function (str, delayTime)
{
Var delayTime = delayTime | 100;
Var regExp_scriptTag = new RegExp ("<\ s * script ([^>] *)> ([\ s \ S] *?) </\ S * script \ s *> "," gi ");
Var regExp_scriptAttrib_src = new RegExp ("\ s * src? \ S * = \ s * (\ "([^ \"] +) \ "| \ '([^ \'] +) \ '| \ s * ([^ \ s] +) \ s *) "," gi ");
Var arr_scriptTag = null;
Var arr_scriptAttib = null;
Var scriptData = "";
Var jsList = new Array ();
While (arr_scripttag=regexp_scripttag.exe c (str ))! = Null)
{
While (arr_scriptattib1_regexp_scriptattrib_src.exe c (arr_scriptTag [1])! = Null)
{
If (arr_scriptAttib [3])
{
JsList. push (arr_scriptAttib [3]);
}
Else if (arr_scriptAttib [2])
{
JsList. push (arr_scriptAttib [2]);
}
Else
{
JsList. push (arr_scriptAttib [1]);
}
}
ScriptData + = (arr_scriptTag [2]);
}
For (var I = 0; I <jsList. length; I ++)
{
Var script = document. createElement ("script ");
Script. src = jsList [I];
Document. body. appendChild (script );
}
If (scriptData. length> 0)
{
Var fn = "_ siteFunction _" + new Date (). getTime () + "_" + parseInt (Math. random () * 10000) + "_ 120 ";
ScriptData = "var" + fn + "= function () {" + scriptData + "};" + fn + "();"
Window. eval (scriptData );
}
}

This is to use Js to process regular expressions. The principle is the same as that of Java,
The function is to read the source code of a webpage and extract and load all the script labels in it.
Here [\ s \ S] *? Use the non-Greedy mode to match all the code between the most recent script tags. Includes line feed

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.