RunKit + npm: whatwg-url

node v18.11.0
endpointsharetweet
const { URL } = require("whatwg-url"); const isValidHost = (host) => { const url = new URL('https://-'); url.host = host; const parsed = url.host; // If input host value is empty or contains [/\], or the processed // host contains more '%' characters, then the input is an invalid // or an improperly-encoded host. return ( host.length > 0 && parsed.length > 0 && // host includes at least one 'letter' or 0-9 digit character // (required reading: https://unicode.org/reports/tr18/#property_syntax ) /[\p{L}\p{Nd}]/u.test(host) && !/[/\\]/.test(host) && (parsed === host.toLowerCase() || (parsed.length !== host.length && [...parsed.matchAll(/%/g)].length === [...host.matchAll(/%/g)].length)) ); }; console.log(isValidHost("example|.com"));
Created from: https://npm.runkit.com/whatwg-url
Loading…