Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

in js, how can i take only a domain and its tld in an arbitrary string that can contains…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o4-mini-2025-04-16’s answer.
  1. User

    in js, how can i take only a domain and its tld in an arbitrary string that can contains anything, like query strings with domains, or sub-domains ?

  2. Assistant

    To extract only the domain and its top-level domain (TLD) from an arbitrary string in JavaScript — ignoring subdomains, query strings, paths, etc. — you can use a combination of:

    1. A regex to find domain-like patterns.
    2. A method to extract the main domain + TLD (e.g., from sub.example.co.uk → example.co.uk).

    Key points:

    • Domains can be tricky because TLDs can be multi-part (like .co.uk).
    • A simple regex can find domain-like strings.
    • To reliably get the "registered domain" (main domain + TLD), you often need a public suffix list or a library.

    Simple approach (basic regex + splitting):

    function extractDomain(input) {
      // Regex to find domain-like patterns (basic)
      const domainRegex = /([a-z0-9-]+\.)+[a-z]{2,}/gi;
      const matches = input.match(domainRegex);
      if (!matches) return null;
    
      // Take the first match (or you can loop through all)
      const fullDomain = matches[0].toLowerCase();
    
      // Split domain parts
      const parts = fullDomain.split('.');
    
      // Handle simple TLD extraction:
      // For example, if domain is "sub.example.com"
      // we want "example.com"
      if (parts.length < 2) return null;
    
      // For simplicity, assume TLD is last part
      // and domain is second last part
      const domain = parts.slice(-2).join('.');
    
      return domain;
    }
    
    // Examples:
    console.log(extractDomain("Visit https://sub.example.com/path?query=123")); // example.com
    console.log(extractDomain("Check out mail.google.co.uk now!")); // co.uk (not perfect)
    

    Limitations:

    • This simple method fails on multi-part TLDs like .co.uk because it just takes last two parts.
    • It will return co.uk instead of google.co.uk in the second example.

    Better approach: Use a library like tldts

    The tldts library can parse domains correctly using the public suffix list.

    npm install tldts
    
    import { parse } from 'tldts';
    
    function extractDomain(input) {
      // Find domain-like strings in input
      const domainRegex = /([a-z0-9-]+\.)+[a-z]{2,}/gi;
      const matches = input.match(domainRegex);
      if (!matches) return null;
    
      // Use tldts to parse and get domain + TLD
      for (const candidate of matches) {
        const parsed = parse(candidate);
        if (parsed.domain) {
          return parsed.domain; // e.g. example.com or google.co.uk
        }
      }
    
      return null;
    }
    
    // Examples:
    console.log(extractDomain("Visit https://sub.example.com/path?query=123")); // example.com
    console.log(extractDomain("Check out mail.google.co.uk now!")); // google.co.uk
    

    Summary:

    • For quick and dirty extraction, use regex + simple split.
    • For robust extraction, use a domain parsing library like tldts.

    If you want, I can help you write a full example with tldts or a pure regex solution!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.