Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

javascript - Regular Expression - Extract subdomain & domain

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://)]([^/]+)

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:

Success

'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)

Failure

'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?

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

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

Edit -

Example:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

RegEx Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...