What is url-regex?
The url-regex npm package is a utility for matching URLs in strings using regular expressions. It can be used to find and extract URLs from text, validate URLs, and more.
What are url-regex's main functionalities?
Match URLs in a string
This feature allows you to find and extract all URLs present in a given string.
const urlRegex = require('url-regex');
const text = 'Check out this link: https://example.com and this one: http://example.org';
const urls = text.match(urlRegex());
console.log(urls); // ['https://example.com', 'http://example.org']
Validate a URL
This feature allows you to validate if a given string is a valid URL.
const urlRegex = require('url-regex');
const isValidUrl = urlRegex({ exact: true }).test('https://example.com');
console.log(isValidUrl); // true
Match URLs with specific protocols
This feature allows you to match URLs with specific protocols, such as 'https' or 'ftp'.
const urlRegex = require('url-regex');
const text = 'Visit our site at https://example.com or our FTP server at ftp://example.org';
const urls = text.match(urlRegex({ protocols: ['https'] }));
console.log(urls); // ['https://example.com']
Other packages similar to url-regex
linkify-it
linkify-it is a fast and lightweight library for finding and converting URLs in plain text into clickable links. It offers more customization options compared to url-regex, such as handling different types of links (e.g., email addresses) and providing hooks for custom link processing.
url-pattern
url-pattern is a utility for matching URLs against patterns. It is more focused on URL routing and matching specific URL structures, making it useful for applications like client-side routing in single-page applications. It offers more flexibility in defining and matching complex URL patterns compared to url-regex.
valid-url
valid-url is a simple utility for validating URLs. It provides functions to check if a URL is valid, if it is a web URL, or if it is an HTTP/HTTPS URL. It is more focused on validation rather than extraction, making it a good choice for scenarios where URL validation is the primary requirement.
url-regex
Regular expression for matching URLs
Based on this gist by Diego Perini.
Install
$ npm install url-regex
Usage
const urlRegex = require('url-regex');
urlRegex().test('http://github.com foo bar');
urlRegex().test('www.github.com foo bar');
urlRegex({exact: true}).test('http://github.com foo bar');
urlRegex({exact: true}).test('http://github.com');
urlRegex({strict: false}).test('github.com foo bar');
urlRegex({exact: true, strict: false}).test('github.com');
'foo http://github.com bar //google.com'.match(urlRegex());
API
urlRegex([options])
Returns a RegExp
for matching URLs.
options
exact
Type: boolean
Default: false
Only match an exact string. Useful with RegExp#test
to check if a string is a URL.
strict
Type: boolean
Default: true
Force URLs to start with a valid protocol or www
. If set to false
it'll match the TLD against a list of valid TLDs.
Related
License
MIT © Kevin Mårtensson and Diego Perini