How to get domain from a string using javascript r

2020-02-29 11:03发布

问题:

As the title suggests, I'm trying to retrieve the domain from a string using javascript regular expression.

Take the following strings:

String                                  ==>     Return
"google"                                ==>     null
"google.com"                            ==>     "google.com"
"www.google.com"                        ==>     "www.google.com"
"ftp://ftp.google.com"                  ==>     "ftp.google.com"
"http://www.google.com"                 ==>     "www.google.com"
"http://www.google.com/"                ==>     "www.google.com"
"https://www.google.com/"               ==>     "www.google.com"
"https://www.google.com.sg/"            ==>     "www.google.com.sg"
"https://www.google.com.sg/search/"     ==>     "www.google.com.sg"
"*://www.google.com.sg/search/"         ==>     "www.google.com.sg"

I've already read "Regex to find domain name without www - Stack Overflow" and "Extract root domain name from string - Stack Overflow" but they were too complicated so I tried writing my own regular expression:

var re = new RegExp("[\\w]+[\\.\\w]+");
/[\w]+[\.\w]+/
re.exec(document.URL);

which works fine with "google.com", "www.google.com" and "www.google.com.sg" but returns http with "http://google.com/", "http://www.google.com/" etc.

As I am new to regular expressions, I can't seem to figure out what's wrong... any ideas?

Thanks in advance!

回答1:

Use this regex:

/(?:[\w-]+\.)+[\w-]+/

Here is a regex demo!

Sampling:

>>> var regex = /(?:[\w-]+\.)+[\w-]+/
>>> regex.exec("google.com")
... ["google.com"]
>>> regex.exec("www.google.com")
... ["www.google.com"]
>>> regex.exec("ftp://ftp.google.com")
... ["ftp.google.com"]
>>> regex.exec("http://www.google.com")
... ["www.google.com"]
>>> regex.exec("http://www.google.com/")
... ["www.google.com"]
>>> regex.exec("https://www.google.com/")
... ["www.google.com"]
>>> regex.exec("https://www.google.com.sg/")
... ["www.google.com.sg"]


回答2:

You can use this regex in Javascript:

\b(?:(?:https?|ftp):\/\/)?([^\/\n]+)\/?

RegEx Demo