捕获给定URL的根域有什么好的正则表达式? [英] What's a good regular expression to capture the root domain of a given url?
本文介绍了捕获给定URL的根域有什么好的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
理想情况下,我想从 http://foo.com等网站上抓取foo.com / bar?param = value 或 https://www.foo.com
Ideally, I'd like to capture foo.com from something like http://foo.com/bar?param=value or https://www.foo.com
谢谢。
编辑:抱歉模糊不清。我将在Javascript中执行此操作。
Sorry for the vagueness. I'll be doing this in Javascript.
第二编辑:我应该发布可怕的第一次尝试:https?:// w?w?w?[。]? [^?/]
2nd I should post my terrible first attempt: 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!
这篇关于捕获给定URL的根域有什么好的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文