Check if absolute URL

OSCC · JavaScript, String, Browser, Regexp · Oct 20, 2020

Checks if the given 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

Recommended snippets

  • Join URL segments

    JavaScript, String

    Joins all given URL segments together, then normalizes the resulting URL.

  • Get base URL

    JavaScript, String

    Gets the current URL without any parameters or fragment identifiers.

  • Checks if the given string contains any whitespace characters.