I have a function that uses regex to return root domain of the given url.
function cleanUp(url) {
url = url.replace(new RegExp(/^\s+/),""); // START
url = url.replace(new RegExp(/\s+$/),""); // END
// IF FOUND, CONVERT BACK SLASHES TO FORWARD SLASHES
url = url.replace(new RegExp(/\\/g),"/");
// IF THERE, REMOVES 'http://', 'https://' or 'ftp://' FROM THE START
url = url.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");
// IF THERE, REMOVES 'www.' FROM THE START OF THE STRING
url = url.replace(new RegExp(/^www\./i),"");
//remove slash from end
url = url.replace(new RegExp(/\/$/i),"");
return url;
}
But it uses multi regex and we are worried about the performance. Is there a better way to do the same in a one line regex?
Note:
document.location.host does not seem to work in my case.