
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
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.
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']
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 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 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.
Regular expression for matching URLs
Based on this gist by Diego Perini.
$ npm install url-regex
const urlRegex = require('url-regex');
urlRegex().test('http://github.com foo bar');
//=> true
urlRegex().test('www.github.com foo bar');
//=> true
urlRegex({exact: true}).test('http://github.com foo bar');
//=> false
urlRegex({exact: true}).test('http://github.com');
//=> true
urlRegex({strict: false}).test('github.com foo bar');
//=> true
urlRegex({exact: true, strict: false}).test('github.com');
//=> true
'foo http://github.com bar //google.com'.match(urlRegex());
//=> ['http://github.com', '//google.com']
Returns a RegExp
for matching URLs.
Type: boolean
Default: false
Only match an exact string. Useful with RegExp#test
to check if a string is a URL.
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.
MIT © Kevin Mårtensson and Diego Perini
FAQs
Regular expression for matching URLs
The npm package url-regex receives a total of 310,147 weekly downloads. As such, url-regex popularity was classified as popular.
We found that url-regex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.