Regular expression for matching URLs
Clone or download
Latest commit 1e9fdb9 Jun 13, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig Tweaks Jun 8, 2016
.gitattributes Tweaks Jun 8, 2016
.gitignore Remove `tlds.json` from `.gitignore` Dec 5, 2015
.travis.yml [BREAKING] Support Node.js >=4 Sep 29, 2016
index.js Match against longest TLDs first Jun 13, 2017
license Tweaks Feb 2, 2015
package.json 4.1.1 Jun 13, 2017
readme.md Add `strict` option (#38) Jun 2, 2017
test.js Add `strict` option (#38) Jun 2, 2017

readme.md

url-regex Build Status

Regular expression for matching URLs

Based on this gist by Diego Perini.

Install

$ npm install --save 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 regex 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