url-regex ![Build Status](https://camo.githubusercontent.com/837c5020c8de8a8cc7743dc887c1f6449089ff1ac02bf0d4ed403e1e61c9840b/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f6b657676612f75726c2d72656765782e7376673f7374796c653d666c6174)
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');
//=> 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']
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
- get-urls - Get all URLs in text
- linkify-urls - Linkify URLs in text
License
MIT © Kevin Mårtensson and Diego Perini