正则表达式 - 提取子域和&域 [英] Regular Expression - Extract subdomain & domain

查看:231
本文介绍了正则表达式 - 提取子域和&域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试形成一个正则表达式(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

这篇关于正则表达式 - 提取子域和&域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆