-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?

7
  • 1
    Just use the substring method to remove the first 8 characters Commented Feb 28, 2023 at 1:33
  • I also want to remove the hostname Commented Feb 28, 2023 at 1:35
  • You may want to start with parsing the url and then split it up instead of using a regex Commented Feb 28, 2023 at 1:38
  • 5
    It sounds like you've not looked at the vast amount of information window.location and URL can give you. All those URLs have the same top level domain. Commented Feb 28, 2023 at 1: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? Commented Feb 28, 2023 at 1:40

1 Answer 1

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/~"))

Sign up to request clarification or add additional context in comments.

9 Comments

What about hoge.co.uk?
Yes it won't work for that. However it looks like he is only interested in hoge.com.
Rather than use a regex, I'd be more inclined to parse the string as a URL and extract the hostname. That way you won't get false positives for things like credentials
@shibata if that is actually a requirement, then your question is far too broad right now. It needs a lot more detail
thank you Eureka!!!!! I love you !!!!!!!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.