-1

Is there a way to get the domain without the hostname from the URL?

I have the following list

const value1 = 'https://localhost:4200/~';
const value2 = 'https://hoge.com/~';
const value3 = 'https://www.hoge.com/~';
const value4 = 'https://wwwtest.hoge.com/~';
const value5 = 'https://www-test.hoge.com/~';

the answer i'm looking for

'localhost:4200/~'; //value1 
'hoge.com/~'; //value2
'hoge.com/~'; //value3
'hoge.com/~'; //value4
'hoge.com/~'; //value5

I tried the following method, but the answer was not correct.

function hogeReplace(value: string): string {
  return value.replace(/^https?:\/\/|www.*\./g, '');
}

console.log(hogeReplace(value1)); // localhost:4200/~
console.log(hogeReplace(value2)); // hoge.com/~
console.log(hogeReplace(value3)); // com/~
console.log(hogeReplace(value4)); // com/~
console.log(hogeReplace(value5)); // com/~

someone answer?

shibata
  • 21
  • 3
  • 1
    Just use the substring method to remove the first 8 characters – Ayudh Feb 28 '23 at 01:33
  • I also want to remove the hostname – shibata Feb 28 '23 at 01:35
  • You may want to start with [parsing the url](https://developer.mozilla.org/en-US/docs/Web/API/URL) and then split it up instead of using a regex – Crowcoder Feb 28 '23 at 01:38
  • 5
    It sounds like you've not looked at the vast amount of information [window.location](https://developer.mozilla.org/en-US/docs/Web/API/Location) and [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) can give you. All those URLs have the same top level domain. – Mike 'Pomax' Kamermans Feb 28 '23 at 01:38
  • _"domain without the hostname"_... aren't they essentially the same thing? TLDs are entirely subjective and would be difficult to parse out. How do you tell the difference between a `.com`, `.co.uk`, `.museum`, or `.i.paid.good.money.for.this`? – Phil Feb 28 '23 at 01:40
  • Does this answer your question? [How to get domain name only using javascript?](https://stackoverflow.com/q/8253136/283366) – Phil Feb 28 '23 at 01:51
  • without the hostname? You mean without the protocol? – epascarello Feb 28 '23 at 02:18

1 Answers1

2

Step by step solution:

I am assuming by "domain name" you mean specifically hoge.com, or any two element domain name?

In which case my recommendation would be:

  1. Chop off the https.

  2. Break up the remaining items by "/". Everything after the first "/" can stay.

  3. What is before that first "/", slice into pieces by ".". Keep only the last two of those.

function hogeReplace(value) {
  const withoutHttp = value.replace(/^https?:\/\//, '');
  const segments = withoutHttp.split("/");
  const hostname = segments[0]
  const localPath = segments.slice(1).join("/")
  const domainName = hostname.replace(/^www[a-zA-Z~-]*/,"")
  return domainName + "/" + localPath
}

console.log(hogeReplace("https://localhost:4200/~"))
console.log(hogeReplace("https://hoge.com/~"))
console.log(hogeReplace("https://www.hoge.com/~"))
console.log(hogeReplace("https://wwwtest.hoge.com/~"))
console.log(hogeReplace("https://www-test.hoge.com/~"))
console.log(hogeReplace("https://www.hoge.co.uk/~"))
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26