Files
30-seconds-of-code/javascript/snippets/is-absolute-url.md
2023-05-01 22:35:56 +03:00

485 B

title, type, tags, cover, dateModified
title type tags cover dateModified
Check if absolute URL snippet
string
browser
regexp
coffee-phone-tray-2 2020-10-20T23:02:01+03:00

Checks if the given string is an absolute URL.

  • Use RegExp.prototype.test() to test if the string is an absolute URL.
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
isAbsoluteURL('https://google.com'); // true
isAbsoluteURL('ftp://www.myserver.net'); // true
isAbsoluteURL('/foo/bar'); // false