Capture the domain till the ending characters $, \\?, /, :
. I need a regex that captures domian.com
in all of these.
domain.com:3000
do
If you actually have valid URLs, this will work:
var urls = [
'http://domain.com:3000',
'http://domain.com?pass=gas',
'http://domain.com/',
'http://domain.com'
];
for (x in urls) {
var a = document.createElement('a');
a.href = urls[x];
console.log(a.hostname);
}
//=> domain.com
//=> domain.com
//=> domain.com
//=> domain.com
Note, using regex for this kind of thing is silly when the language you're using has other built-in methods.
Other properties available on A
elements.
var a = document.createElement('a');
a.href = "http://domain.com:3000/path/to/something?query=string#fragment"
a.protocol //=> http:
a.hostname //=> domain.com
a.port //=> 3000
a.pathname //=> /path/to/something
a.search //=> ?query=string
a.hash //=> #fragment
a.host //=> domain.com:3000
Upon further consideration, I looked into the Node.js docs and found this little gem: url#parse
The code above can be rewritten as:
var url = require('url');
var urls = [
'http://domain.com:3000',
'http://domain.com?pass=gas',
'http://domain.com/',
'http://domain.com'
];
for (x in urls) {
console.log(url.parse(urls[x]).hostname);
}
//=> domain.com
//=> domain.com
//=> domain.com
//=> domain.com
See the revision history of this post if you'd like to see how to solve this problem using jsdom
and nodejs