This article introduces the greedy mode and non-greedy mode of the javascript regular expression to the coders. For more information about the coders, see.
/** ** 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 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