简体   繁体   中英

Regex to match simple domain

I am trying to match a simple domain: example.com

But all combinations of it.

How would I do this to cover:

https://example.com
http://www.example.com
etc.
^https?://([\w\d]+\.)?example\.com$

using code:

var result = /^https?:\/\/([a-zA-Z\d-]+\.){0,}example\.com$/.test('https://example.com');
// result is either true of false

I improved it to match like "http://abexample.com"

You can probably use to just match the domain name part of a URL:

/^(?:https?:\/\/)?(?:[^.]+\.)?example\.com(\/.*)?$

It will match any of following strings:

https://example.com
http://www.example.com
http://example.com
https://example.com
www.example.com
example.com

 

RegEx Demo

RegEx Details:

  • ^ : Start
  • (?:https?:\\/\\/)? : Match http:// or https://
  • (?:[^.]+\\.)? : Optionally Match text till immediately next dot and dot
  • example\\.com : Match example.com
  • (\\/.*)? : Optionally Match / followed by 0 or more of any characters
  • $ : End

A more generic example I used:

/http(?:s)?:\/\/(?:[\w-]+\.)*([\w-]{1,63})(?:\.(?:\w{3}|\w{2}))(?:$|\/)/i

Note that this solution doesn't pick up the correct label for 5 character TLDs. Example:

http://mylabel.co.uk

Would be picked up as 'co' instead of 'mylabel', but

http://mylabel.co

would be matched correctly as 'mylabel'. The regex was good enough for me even with this limitation.

Note that the 63 character limit for the label is an RFC specification. Hope this helps anyone looking for the same answer in the future.

The following works in Java,

^(http:|https:|)[/][/]([^/]+[.])*example.com$

and matches your test cases, and doesn't match cases like

http://www.google.com/http://example.com

This will correctly match the URL for any variation of the below, plus anything after .com

https://example.com
https://www.example.com
http://www.example.com
http://example.com
https://example.com
www.example.com
example.com

Result will be either true or false

const result = /^(http(s)?(:\/\/))?(www\.)?example\.com(\/.*)?$/.test(value); 

The below exp matches for http/htpps/ftp in the first part, though it can also match random 5 letter word like ahfzc but that rarely would be case and they would be ignored by later part of the exp

The second part matches for ww/www and the last part matches for any alphanumeric seperated by '.'. And the last part matches for any 3 character like .com,.in,.org etc.

try this

r'[a-z0-9]{0,5}[\:\/]+[w]{0,3}[\.]+[a-z0-9\-]+[\.]+[a-z0-9]{0,3}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM