strip-www
Remove a leading "www" subdomain from a hostname.
Note: this library uses a rather complex algorithm for parsing hostnames, which can result in bundled file sizes that are larger than you might expect. If all you want is a simple RegExp, use a 1.x
release; but first read about its edge cases.
Installation
Node.js >= 14
is required. To install, type this at the command line:
npm install strip-www
Usage
const stripWWW = require('strip-www');
stripWWW('www.domain.com'); //-> domain.com
stripWWW('www.www.domain.com'); //-> www.domain.com
stripWWW('www.unlisted-tld'); //-> www.unlisted-tld
The hostname must be Punycode encoded, as provided by a URL
:
stripWWW('www.ᄯᄯᄯ.com');
//-> 'www.ᄯᄯᄯ.com'
const url = new URL('http://www.ᄯᄯᄯ.com');
stripWWW(url.hostname);
//-> 'xn--brdaa.com'
If necessary, you can decode after with punycode:
const { toUnicode } = require('punycode');
console.log(toUnicode('xn--brdaa.com'));
//-> 'ᄯᄯᄯ.com'