[Regular expression] greedy mode and non-greedy mode _ Regular expression

Source: Internet
Author: User
Copy Code code 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.exec (str))!= null)
{
while ((Arr_scriptattib=regexp_scriptattrib_src.exec (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 the use of JS to deal with regular expressions, the principle of the same as Java,
The function is to read a section of the source code, and put all the script tags inside it, intercept and load the run.
What's in this [\s\s]*? Use non-greedy mode to match all the code between the most recent script tags. Include line wrapping

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.