This question already has answers here:
My regex is matching too much. How do I make it stop?
                                
                                    (5个答案)
                                
                        
                                去年关闭。
            
                    
我有以下函数,该函数通过URI通过AJAX提取Javascript块并执行它:

function loadViewViaAjax(url, callbackOnSuccess) {
    Ext.Ajax.request({
        url: url,
        success: function(objServerResponse) {
            var responseText = objServerResponse.responseText;
            var scripts, scriptsFinder=/<script[^>]*>([\s\S]+)<\/script>/gi;
            while(scripts=scriptsFinder.exec(responseText)) {
                eval.call(window,scripts[1]);
            }
            if(typeof callbackOnSuccess != 'undefined') {
                callbackOnSuccess.call();
            }
        }
    });
}


例如,它提取此文本,并在SCRIPT标签之间执行广播:

<script type="text/javascript">
    clearExtjsComponent(targetRegion);
    var start_info_panel = new Ext.Panel({
        padding: 10,
        style: "margin: 10px",
        width: 300,
        html: '<h1>Html Item 1</h1>'
    });
    replaceComponentContent(targetRegion, start_info_panel);
</script>


如何更改loadViewViaAjax函数,以执行多个脚本标签之间的所有内容?例如:

<script type="text/javascript">
    clearExtjsComponent(targetRegion);
    var start_info_panel = new Ext.Panel({
        padding: 10,
        style: "margin: 10px",
        width: 300,
        html: '<h1>Html Item 1</h1>'
    });
    replaceComponentContent(targetRegion, start_info_panel);
</script>
<script type="text/javascript">
    clearExtjsComponent(targetRegion_help);
    var help_panel = new Ext.Panel({
        padding: 10,
        style: "margin: 10px",
        width: 300,
        html: '<p>This is the help text.</p>'
    });
    replaceComponentContent(targetRegion_help, help_panel);
</script>

最佳答案

如果我没看错,这是懒惰量词的经典用法:

/<script[^>]*>([\s\S]+?)<\/script>/gi


另请参阅:What is the difference between .*? and .* regular expressions?

08-19 04:05