Browse Source

Match optional scheme for any pattern

pull/3/head
Luke Childs 9 years ago
parent
commit
68a587976a
  1. 3
      src/regex.js
  2. 24
      test/matches.json

3
src/regex.js

@ -9,6 +9,7 @@ const WhitespaceCommaDotOrEndOfLine = `[${escapeChar}s,.]|$`;
const number = `[0-9]`;
// Sections
const optionalScheme = `((https?:)?//)?`;
const hostname = `(${notWhitespaceOrComma}+${escapeChar}.(${validTlds}))`;
const ip = `(${number}{1,3}${escapeChar}.){3}${number}{1,3}`;
const optionalPortNumber = `(:${number}+)?`;
@ -16,6 +17,6 @@ const optionalSlash = `(${escapeChar}/(${notWhitespaceCommaOrDot}*)?)?`;
const endsWithButDontMatch = `(?=${WhitespaceCommaDotOrEndOfLine})`;
// Build
const regex = `(localhost|${hostname}|${ip})${optionalPortNumber}${optionalSlash}${endsWithButDontMatch}`;
const regex = `${optionalScheme}(localhost|${hostname}|${ip})${optionalPortNumber}${optionalSlash}${endsWithButDontMatch}`;
export default new RegExp(regex, 'gi');

24
test/matches.json

@ -47,6 +47,18 @@
"description": "IP address with trailing slash",
"url": "192.168.0.1/"
},
{
"description": "IP address starting with http://",
"url": "http://192.168.0.1"
},
{
"description": "IP address starting with https://",
"url": "https://192.168.0.1"
},
{
"description": "IP address starting with //",
"url": "//192.168.0.1"
},
{
"description": "Localhost",
"url": "localhost"
@ -55,6 +67,18 @@
"description": "Localhost with trailing slash",
"url": "localhost/"
},
{
"description": "Localhost starting with http://",
"url": "http://localhost"
},
{
"description": "Localhost starting with https://",
"url": "https://localhost"
},
{
"description": "Localhost starting with //",
"url": "//localhost"
},
{
"description": "Url with slug",
"url": "url.com/slug"

Loading…
Cancel
Save