DEV Community 👩‍💻👨‍💻

Discussion on: Daily Challenge #89 - Extract domain name from URL

erezwanderman profile image

Javascript, using npm package "tldjs"

const { parse } = require("tldjs");

const regexify = str => {
  return str.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");

const getDomain = url => {
  const parseResult = parse(url);
  if (parseResult.domain) {
    return parseResult.domain.replace(
      RegExp(regexify("." + parseResult.publicSuffix) + "$"),
  } else {
    return parseResult.hostname;
Enter fullscreen mode Exit fullscreen mode

Try it:

zizzyzizzy profile image

Jesus, NO! This is what's wrong with development today. This is equivalent to killing a fly with a Sherman tank.

Please, please, for the love of God and companies everywhere that are sick of obfuscated, confusing unmanageable, unmaintainable and insecure code, please look at the one line pure Javascript code above this answer.

There is absolutely no reason on earth to include a library with hundreds of lines of code to perform a simple operation.

erezwanderman profile image

Your comment is what's wrong with development today. Taking the shortest solution that seems to somehow solve the vague requirements and declaring it solved and secure.
Each of the JS solutions will fail for one of these test URLs: '', '', ''.
Except for mine.