Check if absolute URL

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

More like this

  • URLs in JavaScript

    Snippet collection

    Working with URLs is an essential skill for web developers. This snippet collection covers all the necessary resources to master URLs in JavaScript.

  • 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.