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!
>>> regex.exec("ftp://www.google.com") ... ["ftp.google.com"]
, how'd you get that? haha :) – Rusel