npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

happy-url-regex

1.0.3 • Public • Published

happy-url-regex

A regular expression for matching URLs.

A fork of url-regex by Kevin Mårtensson, which is based on a gist by Diego Perini.

Why fork?

Because of a niche, client-side use case.

Webpack uglification would complain when using the original url-regex package as it contains ES6 arrow functions and template strings. I have opted to fork the repository and use babel rather than transpiling url-regex during webpack build.

Install

$ npm install --save happy-url-regex

or

$ yarn add happy-url-regex

Usage

import happyUrlRegex from 'happy-url-regex'
 
happyUrlRegex().test('http://github.com foo bar')
// true
 
happyUrlRegex().test('www.github.com foo bar')
// true
 
happyUrlRegex({ exact: true }).test('http://github.com foo bar')
// false
 
happyUrlRegex({ exact: true }).test('http://github.com')
// true
 
happyUrlRegex({ strict: false }).test('github.com foo bar')
// true
 
happyUrlRegex({ exact: true, strict: false }).test('github.com')
// true
 
'foo http://github.com bar //google.com'.match(happyUrlRegex())
// ['http://github.com', '//google.com']

API

happyUrlRegex(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

install

npm i happy-url-regex

Downloadsweekly downloads

82

version

1.0.3

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability