English 中文(简体)
来自贾瓦的排泄参数包括使用雷克语的标语
原标题:Extracting parameters from JavaScript include tag using a Regex

I am currently trying to parse parameters from a path to a JavaScript file (inside a script tag). At the moment I know which parameters I expect to be there but instead of looking for the expected params I would rather like to just extract all params given.

包括 Java印文件在内的文字标签实例:

<script type="text/javascript" src="https://url/widget.js?param1=A&param2=bb></script>

在我刚刚做这项工作的时候(每个参数通常):

jQuery( script ).each(function() {
    var script = this;
    if (!script.src) {
        return;
    }    
    var matchKey = script.match(/https://url/widget.js?param1=([A-Z]+)/);
    if (matchKey) {
        oSettings.param1 = matchKey[1];
    }
}

因此,我所需要的是一条可以提取参数名称和所包括管道的价值的节奏。

感谢援助!

最佳回答

测试功能:

function parse_query_vars(text)
{ // Extract name=value pairs from URL query string.
    // Empty object to store name, value pairs.
    var qvars = {},
    // Capture non-empty query string in $1.
        re_q = /?([^#]+)/, // From  ?  up to  #  or EOS.
    // Capture variable name in $1 and value in $2.
        re_nv = /([^=]+)=([^&]*)(?:&(amp;)?|$)/gi,
    // Match array for query string and va=val pairs.
        m =  text.match(re_q),
    // Query string plucked from URL
        q =   ;
    // If there is a query string, copy to q var.
    if (m) q = m[1];
    while (m = re_nv.exec(q)) {
        qvars[m[1]] = m[2];
    }
    return qvars; // Return results in object
}

它首先从URL中提取任何质询,然后从理论上说,在标的上划出名称。 在查询后,如果URL有#fragment,则它处理通过<代码>&或和施工而分离的数值。

问题回答

Use something like this, or this, or this.

他们不仅重新寻找一切解决办法,而且你不一定需要一种reg。 这是一个可能无法解决问题的细节。

希望会有所帮助。

(This isn t actually tested)

var scripts = document.getElementsByTagName("script"), i = scripts.length;
var reMatch = /https://url/widget.js/, path;
// find the correct script
do {
    path = scripts[i--].src;
}
while (!reMatch.test(path));

var map = {}, pairs = path.substring(path.indexOf("?") + 1).split("&"), atoms;
i = pairs.length;
// extract the name-value pairs
while (i--) {
    atoms = pairs[i].split("=");
    map[decodeURIComponent(atoms[0])] = decodeURIComponent(atoms[1]);
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.