[Home] >  String >  Check if a URL is absolute

Check if a URL is absolute

JavaScript version

const isAbsoluteUrl = (url) => /^[a-z][a-z0-9+.-]*:/.test(url);

TypeScript version

const isAbsoluteUrl = (url: string): boolean => /^[a-z][a-z0-9+.-]*:/.test(url);

Examples

isAbsoluteUrl('https://tech.libresinn.com/cheatsheet/'); // true
isAbsoluteUrl('https://tech.libresinn.com/cheatsheet/foo/bar'); // true
isAbsoluteUrl('tech.libresinn.com/cheatsheet/'); // false
isAbsoluteUrl('//tech.libresinn.com/cheatsheet/'); // false