JavaScript urlparse - 类似Python的URL解析器和操纵器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript urlparse - 类似Python的URL解析器和操纵器相关的知识,希望对你有一定的参考价值。

/** 
* @projectDescription 	Poly9's polyvalent URLParser class
* 
* Modified to URL reconstruction by Mikko Ohtamaa.
* 
* @authorDenis Laprise <denis@poly9.com>, Mikko Ohtamaa <mikko.ohtamaa@twinapex.com>
*           
* @version	0.2
* @namespace	Poly9
* 
* How to query URL: 
* 
* <pre>
* var p = new Poly9.URLParser('http://user:password@poly9.com/pathname?arguments=1#fragment');
* p.getHost() == 'poly9.com';
* p.getProtocol() == 'http';
* p.getPathname() == '/pathname';
* p.getQuerystring() == 'arguments=1';
* p.getFragment() == 'fragment';
* p.getUsername() == 'user';
* p.getPassword() == 'password';
* </pre>
* 
* How to manipulate URL:
* 
* <pre>
* var p = new Poly9.URLParser("http://localhost:8080/path);
* 
* p.setQuerystring("foo=bar");
* p.setFragment("anchor");
* p.setPort(7070);
* 
* var url = p.getURL() // http://localhost:7070/path?foo=bar#anchor
* </pre>
* 
* See the unit test file for more examples.
* URLParser is freely distributable under the terms of an MIT-style license.
* 
* The orignal code: https://code.poly9.com/trac/browser/urlparser/urlparser.js
* 
* http://poly9.com
*  
* http://www.twinapex.com
*/

if (typeof Poly9 == 'undefined')
 var Poly9 = {};

/**
 * Creates an URLParser instance
 *
 * @classDescription	Creates an URLParser instance
 * @return {Object}	return an URLParser object
 * @param {String} url	The url to parse
 * @constructor
 * @exception {String}  Throws an exception if the specified url is invalid
 */
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.2;
 
 // MO: Fixed fragments are allowed to contain dash 
 this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?([^#]*)/;
 for (var f in this._fields) {
 	this['get' + f] = this._makeGetter(f);
 	this['set' + f] = this._makeSetter(f);
 }
 
 // Patch in getURL, since otherwise field generators override this
 this.getURL = this._getURL;
 
 if (typeof url != 'undefined')
  this._parse(url);
}
 
/**
 * @method 
 * @param {String} url	The url to parse
 * @exception {String} 	Throws an exception if the specified url is invalid
 */
Poly9.URLParser.prototype.setURL = function(url) {
  this._parse(url);
}

/**
 * Rebuild the URL to string form
 * 
 * @method 
 * @return {String} Reconstructed full URL
 */
Poly9.URLParser.prototype._getURL = function() {
	var s;
	
	s = this.getProtocol();
	
	s += "://"

	if(this.getUsername() != '') {
		s += this.getUsername();		
	}
	
	if(this.getPassword() != '') {
		s += ":";
		s += this.getPassword();
	}
	
	if(this.getUsername() != '') {
		s += "@";
	}
		
	s += this.getHost();
	
	if(this.getPort() != '') {
		s += ":" + this.getPort();
	}
	
	s += this.getPathname();
	
	if(this.getQuerystring() != '') {
		s += "?" + this.getQuerystring();
	}
	
	if(this.getFragment() != '') {
		s += "#" + this.getFragment();
	}
	
	return s;	
}

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._fields[f]];
}

Poly9.URLParser.prototype._makeGetter = function(field) {
 return function() {
  return this._values[field];
 }
}

Poly9.URLParser.prototype._makeSetter = function(field) {
 return function(value) {
   this._values[field] = value;
 }
}

以上是关于JavaScript urlparse - 类似Python的URL解析器和操纵器的主要内容,如果未能解决你的问题,请参考以下文章

python 中 urlparse 模块介绍

python 中 urlparse 模块介绍

【Python中常用的库系列】——操作url的模块urlparse 模块

urlparse模块

urlparse模块(python模块)

Python-urlparse