Check if a string is a URL
npx @tessl/cli install tessl/npm-is-url-superb@6.1.0is-url-superb provides a simple and reliable utility for validating whether a given string is a valid URL. It leverages the native JavaScript URL constructor for validation, ensuring robust and standards-compliant URL checking with support for both strict and lenient validation modes.
npm install is-url-superbimport isUrl from 'is-url-superb';For importing types (when needed):
import isUrl, { type Options } from 'is-url-superb';import isUrl from 'is-url-superb';
// Basic URL validation
isUrl('https://sindresorhus.com');
//=> true
isUrl('unicorn');
//=> false
// Lenient mode - allows URLs without protocol
isUrl('example.com');
//=> false
isUrl('example.com', {lenient: true});
//=> trueValidates whether a string is a properly formatted URL using the native URL constructor.
/**
* Check if a string is a URL
* @param url - The string to validate as a URL
* @param options - Configuration options (optional)
* @returns Returns true if the string is a valid URL, false otherwise
* @throws Throws TypeError if input is not a string
*/
function isUrl(url: string, options?: Options): boolean;Parameters:
url (string): The string to validate as a URLoptions (Options, optional): Configuration options, defaults to {lenient: false} if not providedBehavior:
false if the string contains spacesError Handling:
TypeError if the input is not a stringUsage Examples:
import isUrl from 'is-url-superb';
// Valid URLs
isUrl('https://sindresorhus.com'); // => true
isUrl(' https://sindresorhus.com '); // => true (trims whitespace)
isUrl('http://example.com/path'); // => true
isUrl('ftp://files.example.com'); // => true
// Invalid URLs
isUrl('unicorn'); // => false
isUrl('abc https://sindresorhus.com'); // => false (contains spaces)
isUrl('https://sindresorhus.com abc'); // => false (contains spaces)
isUrl('https://sindresorhus.com/abc def'); // => false (contains spaces)
isUrl('//sindresorhus.com'); // => false (no protocol)
// Lenient mode examples
isUrl('//sindresorhus.com', {lenient: true}); // => true
isUrl('localhost', {lenient: true}); // => true
isUrl('192.168.0.1', {lenient: true}); // => true
isUrl('www.example.com', {lenient: true}); // => true
// Error cases
isUrl(123); // => TypeError: Expected a string
isUrl(null); // => TypeError: Expected a string/**
* Configuration options for URL validation
*/
interface Options {
/**
* Allow URLs without a protocol by automatically prepending 'https://'
* @default false
*/
readonly lenient?: boolean;
}