原创作者: site120
阅读:4188次
评论:0条
更新时间:2011-05-26
js 代码
- /**
- ** 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);
- }
- }
这是用Js来处理正则表达式,原理与Java一样,
功能是读取一段网页源代码,并将它里面所有的script标签,截取并加载运行。
这里面的[\s\S]*?利用非贪婪模式来匹配最近script标签之间的所有代码。包括换行
评论 共 0 条 请登录后发表评论