Fix matching multiple whitespace characters at the end of a string

This commit is contained in:
Alex Gleason 2025-03-12 16:53:22 -05:00
parent 69a9534463
commit 80fcda2b01
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 2 additions and 2 deletions

View file

@ -145,7 +145,7 @@ Deno.test('removeTrailingTokens with newlines', () => {
]); ]);
const result = removeTrailingTokens( const result = removeTrailingTokens(
'Hey!\n\nthis is cool \n\nhttps://ditto.pub/a.png\nhttps://ditto.pub/b.jpg', 'Hey!\n\nthis is cool \n\nhttps://ditto.pub/a.png\nhttps://ditto.pub/b.jpg\n ',
urls, urls,
); );

View file

@ -74,7 +74,7 @@ export function removeTrailingTokens(text: string, tokens: Set<string>): string
let trimmedText = text; let trimmedText = text;
while (true) { while (true) {
const match = trimmedText.match(/\s?([^\s]+)\s?$/); const match = trimmedText.match(/([^\s]+)(?:\s+)?$/);
if (match && tokens.has(match[1])) { if (match && tokens.has(match[1])) {
trimmedText = trimmedText.slice(0, match.index).replace(/\s+$/, ''); trimmedText = trimmedText.slice(0, match.index).replace(/\s+$/, '');
} else { } else {