Capture the domain till the ending characters $, \?, /, :
. I need a regex that captures domian.com
in all of these.
domain.com:3000
domain.com?pass=gas
domain.com/
domain.com
Capture the domain till the ending characters $, \?, /, :
. I need a regex that captures domian.com
in all of these.
domain.com:3000
domain.com?pass=gas
domain.com/
domain.com
If you actually have valid URLs, this will work:
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.EDIT #2
Upon further consideration, I looked into the Node.js docs and found this little gem:
url#parse
The code above can be rewritten as:
EDIT #1
See the revision history of this post if you'd like to see how to solve this problem using
jsdom
andnodejs
I'm using Node
^10
and this is how I extract the hostname from a URL.Since you're using node, just use the built-in
url.parse()
method; you want the resultinghostname
property:UPDATED:
matches are host, port, path
A new challenger has appeared. According to node docs, you can also use
https://nodejs.org/api/url.html#url_the_whatwg_url_api
This seems to be a more current way.