Home > Web Front-end > JS Tutorial > javascript 正则表达式贪婪模式与非贪婪模式

javascript 正则表达式贪婪模式与非贪婪模式

WBOY
Release: 2016-06-01 09:54:09
Original
854 people have browsed it
<code class="language-javascript">/**     
**   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);      
    }      
}   </code>
Copy after login

这是用Js来处理正则表达式,原理与Java一样, 
功能是读取一段网页源代码,并将它里面所有的script标签,截取并加载运行。 
这里面的[\s\S]*?利用非贪婪模式来匹配最近script标签之间的所有代码。包括换行 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template