Extract parameter value from url using regular expressions

后端 未结 6 1664
抹茶落季
抹茶落季 2020-12-08 06:19

This should be very simple (when you know the answer). From this question

I want to give the posted solution a try. My question is:

How to get the parameter

6条回答
  •  鱼传尺愫
    2020-12-08 07:13

    I use seperate custom functions which gets all URL Parameters and URL parts . For URL parameters, (which is the final part of an URI String, http://domain.tld/urlpart/?x=a&y=b

        function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
        }
    

    The above function will return an array consisting of url variables.

    For URL Parts or functions, (which is http://domain.tld/urlpart/?x=a&y=b I use a simple uri split,

    function getUrlParams() { 
        var vars = {};
        var parts = window.location.href.split('/' );
        return parts;
    }
    

    You can even combine them both to be able to use with a single call in a page or in javascript.

提交回复
热议问题