There are two methods of regular expression to obtain the value of the URL parameter of the URL

There are many expressions on the Internet, but they are all a pattern, as follows:

    getUrlParms(name) {
      const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      const r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },

Among them: (^|&): means to match non-'&' characters

           ([^&]*): means matching non-'&' characters 0 or more times, greedy matching

            (&|$): means to match the '&' character or means to end with the previous expression

            r[2]: Indicates the value matched by ([^&]*), that is, the value of the url parameter

Another mode available today:

    getUrlParms(name) {
        const reg = new RegExp('(?<=' + name + '=)[^&]*');
        const r = window.location.href.substr(1).match(reg);
        if (r != null && r != undefined) {
            return unescape(r[0].match("[^\s]+[^]*")[0]);
        }
        return null;
    },

Among them: (?<=' + name + '=): Indicates reverse positive lookup of 'name parameter value' + '='. is a non-acquiring match that can match: '? 'name=' in name=agme'

[^&]*: means matching non-'&' characters 0 or more times, greedy matching

Finally, the matched value of '(?<=' + name + '=)[^&]*' is the parameter value to be obtained in the url. At the same time, in order to remove the blank characters in the parameter value, another '[^\s]+[^]*' match is made

Guess you like

Origin blog.csdn.net/Alan_Walker1/article/details/125522958