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.
- get-urls - Get all URLs in text
- linkify-urls - Linkify URLs in text
MIT © Kevin Mårtensson and Diego Perini