-5

Let's say I have a string like this

let content = "https://stackoverflow.com/questions/ask";

Now I want to write a regex what will give me only the actual domain with the protocol

example: from content, I should get only "https://stackoverflow.com"

Terry
  • 63,248
  • 15
  • 96
  • 118
Mizan
  • 3
  • 2

1 Answers1

0

No need to write a regex when the ability to parse URLs already exist.

MDN: Web technology for developers > See Web APIs > URL

const url = new URL("https://stackoverflow.com/questions/ask");

console.log(`${url.origin}`);                    // Includes port
console.log(`${url.protocol}//${url.hostname}`); // Alternatively...
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132