Regular instance sharing with matching URL parameters

Source: Internet
Author: User
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} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.