Repository: kevva/url-regex Branch: master Commit: 9f0450b59906 Files: 12 Total size: 10.8 KB Directory structure: gitextract_snapf56u/ ├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 ================================================ FILE: .gitattributes ================================================ * text=auto eol=lf ================================================ FILE: .gitignore ================================================ node_modules yarn.lock ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: .travis.yml ================================================ language: node_js node_js: - '10' - '8' ================================================ FILE: index.d.ts ================================================ declare namespace urlRegex { interface Options { /** Only match an exact string. Useful with `RegExp#test` to check if a string is a URL. @default false */ readonly exact?: boolean; /** 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](https://github.com/stephenmathieson/node-tlds). @default true */ readonly strict?: boolean; } } /** Regular expression for matching URLs. @example ``` import 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'] ``` */ declare function urlRegex(options?: urlRegex.Options): RegExp; export = urlRegex; ================================================ FILE: index.js ================================================ 'use strict'; const ipRegex = require('ip-regex'); const tlds = require('tlds'); module.exports = options => { options = { strict: true, ...options }; const protocol = `(?:(?:[a-z]+:)?//)${options.strict ? '' : '?'}`; const auth = '(?:\\S+(?::\\S*)?@)?'; const ip = ipRegex.v4().source; const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)'; const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*'; const tld = `(?:\\.${options.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`; const port = '(?::\\d{2,5})?'; const path = '(?:[/?#][^\\s"]*)?'; const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`; return options.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig'); }; ================================================ FILE: index.test-d.ts ================================================ import {expectType} from 'tsd'; import urlRegex = require('.'); expectType(urlRegex()); expectType(urlRegex({exact: true})); expectType(urlRegex({strict: false})); ================================================ FILE: license ================================================ The MIT License (MIT) Copyright (c) Kevin Mårtensson and Diego Perini Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: package.json ================================================ { "name": "url-regex", "version": "5.0.0", "description": "Regular expression for matching URLs", "license": "MIT", "repository": "kevva/url-regex", "author": { "name": "Kevin Mårtensson", "email": "kevinmartensson@gmail.com", "url": "https://github.com/kevva" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "regex", "string", "url" ], "dependencies": { "ip-regex": "^4.1.0", "tlds": "^1.203.0" }, "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.2", "xo": "^0.24.0" } } ================================================ FILE: readme.md ================================================ # url-regex [![Build Status](http://img.shields.io/travis/kevva/url-regex.svg?style=flat)](https://travis-ci.org/kevva/url-regex) > Regular expression for matching URLs Based on this [gist](https://gist.github.com/dperini/729294) by Diego Perini. ## Install ``` $ npm install url-regex ``` ## Usage ```js 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](https://github.com/stephenmathieson/node-tlds). ## Related - [get-urls](https://github.com/sindresorhus/get-urls) - Get all URLs in text - [linkify-urls](https://github.com/sindresorhus/linkify-urls) - Linkify URLs in text ## License MIT © [Kevin Mårtensson](https://github.com/kevva) and [Diego Perini](https://github.com/dperini) ================================================ FILE: test.js ================================================ import test from 'ava'; import urlRegex from '.'; test('match exact URLs', t => { const fixtures = [ 'http://foo.com/blah_blah', 'http://foo.com/blah_blah/', 'http://foo.com/blah_blah_(wikipedia)', 'http://foo.com/blah_blah_(wikipedia)_(again)', 'http://www.example.com/wpstyle/?p=364', 'https://www.example.com/foo/?bar=baz&inga=42&quux', 'http://a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.com', 'http://a_b.z.com', 'http://mw1.google.com/mw-earth-vectordb/kml-samples/gp/seattle/gigapxl/$[level]/r$[y]_c$[x].jpg', 'http://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body', 'http://www.microsoft.xn--comindex-g03d.html.irongeek.com', 'http://✪df.ws/123', 'http://localhost/', 'http://userid:password@example.com:8080', 'http://userid:password@example.com:8080/', 'http://userid@example.com', 'http://userid@example.com/', 'http://userid@example.com:8080', 'http://userid@example.com:8080/', 'http://userid:password@example.com', 'http://userid:password@example.com/', 'http://142.42.1.1/', 'http://142.42.1.1:8080/', 'http://➡.ws/䨹', 'http://⌘.ws', 'http://⌘.ws/', 'http://foo.com/blah_(wikipedia)#cite-1', 'http://foo.com/blah_(wikipedia)_blah#cite-1', 'http://foo.com/unicode_(✪)_in_parens', 'http://foo.com/(something)?after=parens', 'http://☺.damowmow.com/', 'http://code.google.com/events/#&product=browser', 'http://j.mp', 'ftp://foo.bar/baz', 'http://foo.bar/?q=Test%20URL-encoded%20stuff', 'http://مثال.إختبار', 'http://例子.测试', 'http://उदाहरण.परीक्षा', 'http://-.~_!$&\'()*+\';=:%40:80%2f::::::@example.com', 'http://1337.net', 'http://a.b-c.de', 'http://223.255.255.254', 'http://example.com?foo=bar', 'http://example.com#foo', 'ws://localhost:8080', 'ws://foo.ws', 'ws://a.b-c.de', 'ws://223.255.255.254', 'ws://userid:password@example.com', 'ws://➡.ws/䨹', '//localhost:8080', '//foo.ws', '//a.b-c.de', '//223.255.255.254', '//userid:password@example.com', '//➡.ws/䨹', 'www.google.com/unicorn', 'http://example.com.' ]; for (const x of fixtures) { t.true(urlRegex({exact: true}).test(x)); } }); test('match URLs in text', t => { const fixture = ` Lorem ipsum //dolor.sit example.com with path [and another](https://another.example.com) and Foo //bar.net/?q=Query with spaces `; t.deepEqual([ '//dolor.sit', 'http://example.com', 'http://example.com/with-path', 'https://another.example.com', '//bar.net/?q=Query' ], fixture.match(urlRegex())); }); test('do not match URLs', t => { const fixtures = [ 'http://', 'http://.', 'http://..', 'http://../', 'http://?', 'http://??', 'http://??/', 'http://#', 'http://##', 'http://##/', 'http://foo.bar?q=Spaces should be encoded', '//', '//a', '///a', '///', 'http:///a', 'foo.com', 'rdar://1234', 'h://test', 'http:// shouldfail.com', ':// should fail', 'http://foo.bar/foo(bar)baz quux', 'http://-error-.invalid/', 'http://-a.b.co', 'http://a.b-.co', 'http://123.123.123', 'http://3628126748', 'http://.www.foo.bar/', 'http://.www.foo.bar./', 'http://go/ogle.com', 'http://foo.bar/ /', 'http://a.b_z.com', 'http://ab_.z.com', 'http://google\\.com', 'http://www(google.com', 'http://www.example.xn--overly-long-punycode-test-string-test-tests-123-test-test123/', 'http://www=google.com', 'https://www.g.com/error\n/bleh/bleh', 'rdar://1234', '/foo.bar/', '///www.foo.bar./' ]; for (const x of fixtures) { t.false(urlRegex({exact: true}).test(x)); } }); test('match using list of TLDs', t => { const fixtures = [ 'foo.com/blah_blah', 'foo.com/blah_blah/', 'foo.com/blah_blah_(wikipedia)', 'foo.com/blah_blah_(wikipedia)_(again)', 'www.example.com/wpstyle/?p=364', 'www.example.com/foo/?bar=baz&inga=42&quux', 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.com', 'mw1.google.com/mw-earth-vectordb/kml-samples/gp/seattle/gigapxl/$[level]/r$[y]_c$[x].jpg', 'user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body', 'www.microsoft.xn--comindex-g03d.html.irongeek.com', '✪df.ws/123', 'localhost/', 'userid:password@example.com:8080', 'userid:password@example.com:8080/', 'userid@example.com', 'userid@example.com/', 'userid@example.com:8080', 'userid@example.com:8080/', 'userid:password@example.com', 'userid:password@example.com/', '142.42.1.1/', '142.42.1.1:8080/', '➡.ws/䨹', '⌘.ws', '⌘.ws/', 'foo.com/blah_(wikipedia)#cite-1', 'foo.com/blah_(wikipedia)_blah#cite-1', 'foo.com/unicode_(✪)_in_parens', 'foo.com/(something)?after=parens', '☺.damowmow.com/', 'code.google.com/events/#&product=browser', 'j.mp', 'foo.bar/baz', 'foo.bar/?q=Test%20URL-encoded%20stuff', '-.~_!$&\'()*+\';=:%40:80%2f::::::@example.com', '1337.net', 'a.b-c.de', '223.255.255.254', 'example.com?foo=bar', 'example.com#foo', 'localhost:8080', 'foo.ws', 'a.b-c.de', '223.255.255.254', 'userid:password@example.com', '➡.ws/䨹', '//localhost:8080', '//foo.ws', '//a.b-c.de', '//223.255.255.254', '//userid:password@example.com', '//➡.ws/䨹', 'www.google.com/unicorn', 'example.com.' ]; for (const x of fixtures) { t.true(urlRegex({exact: true, strict: false}).test(x)); } });