This article mainly and everyone to share the matching URL parameters of the regular instance, I hope to help everyone.
/([^?&=]+) = ([^?&=]*)/g
Explain:
Before and after the slash/is the delimiter of the regular expression, the last G for the global match, match to the first after the stop will not be stopped, the match, the equivalent of PHP preg_match_all, no G is equivalent to Preg_match, below are examples.
() represents a subgroup.
[^] denotes character class inversion, such as [^ABC] matches a single character that is not A and B and C, [^?&=] matches a single character that is not ^ and? and =.
The + and * after the character class [] represent quantifiers:
+ equivalent to {1,} repeated 1 or more times
* Equivalent to {0,} repeated 0 or more times
var str = "Foo=bar&lang=js"; var reg =/[^?&=]{1,}=[^?&=]{0,}/g;console.log (Str.match (reg)); Output ["Foo=bar", "lang=js"]var str = "Foo=bar&lang=js"; var reg =/[^?&=]{1,}=[^?&=]{0,}/;console.log ( Str.match (reg)); Output ["Foo=bar"]var str = "Foo=bar&lang=js"; var reg =/([^?&=]{1,}) = ([^?&=]{0,})/;console.log (Str.match ( REG)); Output ["Foo=bar", "foo", "Bar"]//below is the code that extracts the parameter part to the object
var getqueryobject=function (URL) {url=url==null?window.location.href:url var Search=url.substring (Url.lastindexof ("?") +1) var obj={}//^?&= match not? or & or = single character var reg=/([^?&=]+) = ([^?&=]*)/g search.replace (reg,function (rs,$1,$2) {debugger var name=decodeuricomponent (arguments[1]) var val=decodeuricomponent (arguments[2]) obj[name]=val ret URN}) return obj}