Using javascript, how can I capture the string example.com
from the current domain http://example.com/bar?param=value
or https://www.example.com
?
My first attempt doesn't work correctly:
https?://w?w?w?[.]?[^?/]
You don't need regex for this! Let the browser parse it for you:
var a = document.createElement('a');
a.href = 'http://foo.com/bar?param=value';
a.hostname; // "foo.com"
Voila!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With