Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"';
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});

Expand Down