From 640a7a676622c8c4cb2fdea367f6cdcd97ec0472 Mon Sep 17 00:00:00 2001 From: AilPhaune <109510657+AilPhaune@users.noreply.github.com> Date: Sat, 8 Mar 2025 14:26:55 +0000 Subject: [PATCH] fix: match ipv6 urls --- src/index.js | 6 +++--- test/test.js | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 68e0586..44d070f 100644 --- a/src/index.js +++ b/src/index.js @@ -60,8 +60,8 @@ module.exports = (options) => { options.strict ? strictTld : options.tlds - ? `(?:${options.tlds.sort((a, b) => b.length - a.length).join('|')})` - : defaultTlds + ? `(?:${options.tlds.sort((a, b) => b.length - a.length).join('|')})` + : defaultTlds })${options.trailingPeriod ? '\\.?' : ''}`; let disallowedChars = '\\s"'; @@ -86,7 +86,7 @@ module.exports = (options) => { let regex = `(?:${protocol}|www\\.)${auth}(?:`; if (options.localhost) regex += 'localhost|'; if (options.ipv4) regex += `${ipv4}|`; - if (options.ipv6) regex += `${ipv6}|`; + if (options.ipv6) regex += `${ipv6}|\\[${ipv6}\\]|`; regex += `${host}${domain}${tld})${port}${path}`; // Add option to return the regex string instead of a RegExp diff --git a/test/test.js b/test/test.js index 4593804..a3ea367 100644 --- a/test/test.js +++ b/test/test.js @@ -444,11 +444,13 @@ test('do not match URLs with non-strict mode', (t) => { test('IPv4', (t) => { t.true(urlRegex().test('1.1.1.1')); + t.deepEqual(urlRegex().match('http://127.0.0.1/'), ['http://127.0.0.1/']); t.false(urlRegex({ ipv4: false }).test('1.1.1.1')); }); test('IPv6', (t) => { t.true(urlRegex().test('2606:4700:4700::1111')); + t.deepEqual(urlRegex().match('http://[::1]/'), ['http://[::1]/']); t.false(urlRegex({ ipv6: false }).test('2606:4700:4700::1111')); });