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

问题描述:

我正在尝试形成一个正则表达式(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 for protocol&排除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)

我只使用结果数组中的第一个元素。我无法理解为什么玩。 &安培; T播放。不起作用。有人可以帮我这方面吗?

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 => mail.google .com

你的正则表达式似乎不正确。试试这个正则表达式:

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

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