Skip to content

Commit ad2a39b

Browse files
authoredAug 15, 2023
Merge pull request #29 from birtles/trailing-periods
fix: don't match a trailing period after the path
2 parents 3228299 + f745e26 commit ad2a39b

File tree

2 files changed

+89
-24
lines changed

2 files changed

+89
-24
lines changed
 

Diff for: ‎src/index.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@ module.exports = (options) => {
4545
})${options.trailingPeriod ? '\\.?' : ''}`;
4646

4747
const port = '(?::\\d{2,5})?';
48-
// Not accept closing parenthesis
49-
// <https://github.com/kevva/url-regex/pull/35>
50-
// Don't allow apostrophes
51-
// <https://github.com/kevva/url-regex/pull/55>
52-
const path = options.parens
53-
? options.apostrophes
54-
? '(?:[/?#][^\\s"]*)?'
55-
: '(?:[/?#][^\\s"\']*)?'
56-
: options.apostrophes
57-
? '(?:[/?#][^\\s"\\)]*)?'
58-
: '(?:[/?#][^\\s"\\)\']*)?';
48+
let disallowedChars = '\\s"';
49+
if (!options.parens) {
50+
// Not accept closing parenthesis
51+
// <https://github.com/kevva/url-regex/pull/35>
52+
disallowedChars += '\\)';
53+
}
54+
55+
if (!options.apostrophes) {
56+
// Don't allow apostrophes
57+
// <https://github.com/kevva/url-regex/pull/55>
58+
disallowedChars += "'";
59+
}
60+
61+
const path = options.trailingPeriod
62+
? `(?:[/?#][^${disallowedChars}]*)?`
63+
: `(?:(?:[/?#][^${disallowedChars}]*[^${disallowedChars}.?!])|[/])?`;
5964

6065
// Added IPv6 support
6166
// <https://github.com/kevva/url-regex/issues/60>

Diff for: ‎test/test.js

+73-13
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,77 @@ test('localhost', (t) => {
500500
);
501501
});
502502

503-
test('trailing period', (t) => {
504-
t.deepEqual(
505-
'background example.com. foobar.com'.match(
506-
urlRegex({ trailingPeriod: true })
507-
),
508-
['example.com.', 'foobar.com']
509-
);
510-
t.deepEqual(
511-
'background example.com. foobar.com'.match(
512-
urlRegex({ trailingPeriod: false })
513-
),
503+
for (const [source, withTrailingPeriod, withoutTrailingPeriod] of [
504+
[
505+
'background example.com. foobar.com',
506+
['example.com.', 'foobar.com'],
514507
['example.com', 'foobar.com']
515-
);
516-
});
508+
],
509+
[
510+
'https://example.com/dir.',
511+
['https://example.com/dir.'],
512+
['https://example.com/dir']
513+
],
514+
[
515+
'https://example.com/dir. ',
516+
['https://example.com/dir.'],
517+
['https://example.com/dir']
518+
],
519+
[
520+
'https://example.com/dir.\n',
521+
['https://example.com/dir.'],
522+
['https://example.com/dir']
523+
],
524+
[
525+
'https://example.com/index.html',
526+
['https://example.com/index.html'],
527+
['https://example.com/index.html']
528+
],
529+
[
530+
'https://example.com/index.html.',
531+
['https://example.com/index.html.'],
532+
['https://example.com/index.html']
533+
],
534+
[
535+
'https://example.com/dir.with.dot/.',
536+
['https://example.com/dir.with.dot/.'],
537+
['https://example.com/dir.with.dot/']
538+
],
539+
// Question marks
540+
['Have you ever visited example.com?', ['example.com?'], ['example.com']],
541+
['example.com/?', ['example.com/?'], ['example.com/']],
542+
[
543+
'https://example.com/dir?',
544+
['https://example.com/dir?'],
545+
['https://example.com/dir']
546+
],
547+
// Exclamation marks
548+
['You should check out example.com!', ['example.com'], ['example.com']],
549+
['Here is example.com/!', ['example.com/!'], ['example.com/']],
550+
[
551+
'https://example.com/dir/!',
552+
['https://example.com/dir/!'],
553+
['https://example.com/dir/']
554+
],
555+
[
556+
'https://example.com/dir!',
557+
['https://example.com/dir!'],
558+
['https://example.com/dir']
559+
]
560+
]) {
561+
const sourceTitle = source.replace('\n', '\\n');
562+
563+
test(`trailingPeriod: true (${sourceTitle})`, (t) => {
564+
t.deepEqual(
565+
source.match(urlRegex({ trailingPeriod: true })),
566+
withTrailingPeriod
567+
);
568+
});
569+
570+
test(`trailingPeriod: false (${sourceTitle})`, (t) => {
571+
t.deepEqual(
572+
source.match(urlRegex({ trailingPeriod: false })),
573+
withoutTrailingPeriod
574+
);
575+
});
576+
}

0 commit comments

Comments
 (0)
Please sign in to comment.