简体   繁体   English

使用正则表达式从JavaScript包含标签中提取参数

[英]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). 我目前正在尝试从路径解析参数到JavaScript文件(在script标记内部)。 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. 目前,我知道我希望有哪些参数,但是我不想提取期望的参数,而是想提取所有给定的参数。

Example of the script tag which includes a JavaScript file: 包含JavaScript文件的脚本标签示例:

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

At the moment I'm just doing this (seperately for each parameter): 目前,我只是这样做(分别针对每个参数):

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];
    }
}

So what I need is a regex that extracts both the name of the parameter and the value from the included sript. 因此,我需要的是一个正则表达式,可以从所包含的sript中提取参数的名称和值。

Thanks for the assistance! 感谢您的协助!

This tested function works: 此测试功能起作用:

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
}

It first extracts any query string from the URL, then iteratively parses out name=value pairs and returns the results in an object. 它首先从URL中提取任何查询字符串,然后迭代解析出“名称=值”对,然后将结果返回到一个对象中。 It handles name value pairs separated by either & or &amp; 它处理由&&amp;分隔的名称值对&amp; and works if the URL has a #fragment following the query. 如果网址在查询后带有#fragment则可以使用。

Use something like this , or this , or this . 使用像这样 ,还是这个 ,还是这个

They're not all regex solutions, but then you don't necessarily need a regex. 它们并不是全部的正则表达式解决方案,但是您不一定需要正则表达式。 That was a detail that could probably have been left out of the question. 这是一个细节,可能不予考虑。

Hope that helps. 希望有所帮助。

(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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM