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!
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"]
来源:https://stackoverflow.com/questions/25323126/how-to-get-domain-from-a-string-using-javascript-regular-expression