正则表达式 - 提取子域 &领域

问题描述:

我正在尝试形成一个正则表达式 (javascript/node.js),它将提取子域 &来自任何给定 URL 的域部分.这就是我最终的结果:

I'm trying to form a regular expression (javascript/node.js) which will extract the sub-domain & domain part from any given URL. This is what I ended up with:

[^(?:http://|www.|https://)]([^/]+)

现在,我只是在考虑使用 http、https 作为协议和排除www".来自 URL 的子域 + 域部分的部分.我检查了表达式 &它几乎有效.但是,问题来了:

Right now, I'm just considering http, https for protocol & exclude "www." portion from the subdomain+domain portion of an URL. I checked the expression & it almost works. But, here is the issue:

成功

'http://mplay.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http://|www.|https://)]([^/]+)/i)

'http://lplay.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http://|www.|https://)]([^/]+)/i)

失败

'http://play.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http://|www.|https://)]([^/]+)/i)

'http://tplay.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http://|www.|https://)]([^/]+)/i)

我只使用结果数组中的第一个元素.我不明白为什么要玩".&玩."不起作用.任何人都可以在这方面帮助我吗?

I just use the first element from the result array. I'm not able to understand why "play." & "tplay." doesn't work. Could anyone please help me in this regard?

/p"和/t"对正则表达式求值器有什么意义吗?

Does "/p" and "/t" have any meaning for the regular expression evaluator?

有没有其他方法可以提取子域&使用正则表达式从任何给定 URL 获取域?

Is there any other way of extracting sub-domain & domain from any given URL using a regular expression?

编辑 -

示例:

https://play.google.com/store/apps/details?id=com.skgames.trafficracer => play.google.com

https://mail.google.com/mail/u/0/#inbox=> ma​​il.google.com

您的正则表达式似乎不正确.试试这个正则表达式:

Your regex doesn't seem correct. Try this regex:

/^(?:https?://)?(?:[^@
]+@)?(?:www.)?([^:/
?]+)/img