jsUri
URI parsing and manipulation for node.js and the browser.
Pass any URL into the constructor:
var uri = 'http://user:pass@www.test.com:81/index.html?q=books#fragment'
Use property methods to get at the various parts:
uriprotocol // httpuri // user:passurihost // www.test.comuriport // 81uri // /index.htmluri // q=booksuri // fragment
Property methods accept an optional value to set:
uriprotocol'https'uri // https://user:pass@www.test.com:81/index.html?q=books#fragment urihost'mydomain.com'uri // https://user:pass@mydomain.com:81/index.html?q=books#fragment
Chainable setter methods help you compose strings:
// /archives/1979?page=1 // https://username:password@www.test.com:8080/index.html?this=that&some=thing#content 'http://www.test.com' // https://www.yahoo.com
Query param methods
Returns the first query param value for the key:
'?cat=1&cat=2&cat=3' // 1
Returns all query param values for the given key:
'?cat=1&cat=2&cat=3' // [1, 2, 3]
Internally, query key/value pairs are stored as a series of two-value arrays in the Query object:
'?a=b&c=d'params // [ ['a', 'b'], ['c', 'd']]
Add query param values:
// ?q=books 'http://www.github.com' // http://www.github.com/?testing=123&one=1 // insert param at index 0'?b=2&c=3&d=4' // ?a=1&b=2&c=3&d=4
Replace every query string parameter named key
with newVal
:
// ?page=2 '?a=1&b=2&c=3' // ?a=eh&b=2&c=3 '?a=1&b=2&c=3&c=4&c=5&c=6' // ?a=1&b=2&c=3&c=4&c=five&c=6
Removes instances of query parameters named key
:
'?a=1&b=2&c=3' // ?b=2&c=3 'test.com?a=1&b=2&c=3&a=eh' // test.com/?a=1&b=2&c=3
Test for the existence of query parameters named key
:
'?a=1&b=2&c=3' // true '?a=1&b=2&c=3' // false
Create an identical URI object with no shared state:
var baseUri = 'http://localhost/' baseUri // https://localhost/baseUri // http://localhost/
This project incorporates the parseUri regular expression by Steven Levithan.