Get querystring array values in Javascript

This question already has an answer here:

  • How can I get query string values in JavaScript? 73 answers

  • 你去那里:http://jsfiddle.net/mm6Bt/1/

    function getURLParam(key,target){
        var values = [];
        if (!target) target = location.href;
    
        key = key.replace(/[[]/, "[").replace(/[]]/, "]");
    
        var pattern = key + '=([^&#]+)';
        var o_reg = new RegExp(pattern,'ig');
        while (true){
            var matches = o_reg.exec(target);
            if (matches && matches[1]){
                values.push(matches[1]);
            } else {
                break;
            }
        }
    
        if (!values.length){
            return null;   
        } else {
            return values.length == 1 ? values[0] : values;
        }
    }
    
    var str = 'http://www.example.com?name[]=hello&name[]=world&var1=stam';
    
    console.log(getURLParam('name[]',str));
    console.log(getURLParam('var1',str));
    
    链接地址: http://www.djcxy.com/p/17612.html

    上一篇: 如何使用javascript获取HTTP GET请求值

    下一篇: 在Javascript中获取querystring数组值