Parse class _ Regular expression with JavaScript to implement URL link

Source: Internet
Author: User

Using  Javascript  parsing links (URLs) is a common requirement, and this article describes a very sound,  Javascript -written link (URL) parsing class that can accurately capture a complete   The contents of each part of the url , including information such as the protocol, the user name and password contained in the URL, the hostname, port, pathname, parameters, Anchor Point (Fragment anchor), and so on.  

<body> <div id= "Example" > <div id= "Example_main" > <script type= "Text/javascript" > If (typeof Poly9 = = ' undefined ') {var Poly9 = {}; } poly9.urlparser = function (URL) {this._fields = {' Username ': 4, ' Password ': 5, ' Port ': 7, ' Protocol ': 2, ' Host ': 6, ' Pathname ': 8, ' URL ': 0, ' QueryString ': 9, ' Fragment ': 10}; This._values = {}; This._regex = null; This.version = 0.1; This._regex =/^ ((\w+): \/\/)? ((\w+):? (\w+)? ([^\/\?:]+):? (\d+)? (\/? [^\?#]+)?\?? ([^#]+)?#? (\w*)/; For (var f in this._fields) {this[' get ' + f] = This._makegetter (f); } if (typeof url!= ' undefined ') {this._parse (URL); } Poly9.URLParser.prototype.setURL = function (URL) {this._parse (URL); } poly9.urlparser.prototype._initvalues = function () {for (VAR f in This._fields) {this._values[f] = '; } poly9.urlparser.prototype._parse = function (URL) {this._initvalues (); var r = this._regex.exec (URL); if (!r) throw "Dpurlparser::_parse-> Invalid URL"; For (var f in This._fields) if (typeof R[this._fields[f]]!= ' undefined ') {this._values[f] = R[this._field S[F]]; } poly9.urlparser.prototype._makegetter = function (field) {return function () {return this._values[ Field]; } var url = ' Http://user:password@www.jb51.net:1234/test/test.asp?id=1#test '; var p = new Poly9.urlparser (URL); document.write ("<strong>URL:</strong>" + URL + "<br><br>"); document.write ("Analytic result follows:<br><br>"); document.write ("<strong> Agreement:</strong>" + p.getprotocol () + "<br>"); document.write ("<strong> user:</strong>" + p.getusername () + "<br>"); document.write ("<strong> password:</strong> "+ p.getpassword () +" <br> "); document.write ("<strong> host:</strong>" + p.gethost () + "<br>"); document.write ("<strong> port:</strong>" + p.getport () + "<br>"); document.write ("<strong> path:</strong>" + p.getpathname () + "<br>"); document.write ("<strong> query string:</strong>" + p.getquerystring () + "<br>"); document.write ("<strong> anchor point:</strong>" + p.getfragment () + "<br>"); </script> </div> </div> </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.