From a82efa01b54df7cee4887ed8716d7ec6d897f2ce Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 6 Feb 2026 09:57:38 +0100 Subject: [PATCH] fix: restrict numeric date regex matching to text boundaries (#2195) --- frontend/src/helpers/time/parseDate.ts | 16 +++++++++------- frontend/src/modules/parseTaskText.test.ts | 1 - 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/frontend/src/helpers/time/parseDate.ts b/frontend/src/helpers/time/parseDate.ts index a78b7a865..0ad5ceae2 100644 --- a/frontend/src/helpers/time/parseDate.ts +++ b/frontend/src/helpers/time/parseDate.ts @@ -163,11 +163,13 @@ const addTimeToDate = (text: string, date: Date, previousMatch: string | null): } export const getDateFromText = (text: string, now: Date = new Date()) => { - const dateRegexes: RegExp[] = [ - /(^| )(?(?[0-9][0-9]?)\/(?[0-9][0-9]?)(\/(?[0-9][0-9]([0-9][0-9])?))?)($| )/gi, - /(^| )(?(?[0-9][0-9][0-9][0-9]?)\/(?[0-9][0-9]?)\/(?[0-9][0-9]))($| )/gi, - /(^| )(?(?[0-9][0-9][0-9][0-9]?)-(?[0-9][0-9]?)-(?[0-9][0-9]))($| )/gi, - /(^| )(?(?[0-9][0-9]?)\.(?[0-9][0-9]?)(\.(?[0-9][0-9]([0-9][0-9])?))?)($| )/gi, + // Each entry is the inner date pattern (the part between the boundary anchors). + // Original regexes were: /(^| )(?PATTERN)($| )/gi + const datePatterns: string[] = [ + '(?(?[0-9][0-9]?)\\/(?[0-9][0-9]?)(\\/(?[0-9][0-9]([0-9][0-9])?))?)', + '(?(?[0-9][0-9][0-9][0-9]?)\\/(?[0-9][0-9]?)\\/(?[0-9][0-9]))', + '(?(?[0-9][0-9][0-9][0-9]?)-(?[0-9][0-9]?)-(?[0-9][0-9]))', + '(?(?[0-9][0-9]?)\\.(?[0-9][0-9]?)(\\.(?[0-9][0-9]([0-9][0-9])?))?)', ] let result: string | null = null @@ -176,8 +178,8 @@ export const getDateFromText = (text: string, now: Date = new Date()) => { let containsYear = true // 1. Try parsing the text as a "usual" date, like 2021-06-24 or "06/24/2021" or "27/01" or "01/27" - for (const dateRegex of dateRegexes) { - results = dateRegex.exec(text) + for (const datePattern of datePatterns) { + results = matchDateAtBoundary(text, datePattern, 'gi') if (results !== null) { const {day, month, year, found} = {...results.groups} let tmp_year = year diff --git a/frontend/src/modules/parseTaskText.test.ts b/frontend/src/modules/parseTaskText.test.ts index 973eff107..9d9ca421b 100644 --- a/frontend/src/modules/parseTaskText.test.ts +++ b/frontend/src/modules/parseTaskText.test.ts @@ -367,7 +367,6 @@ describe('Parse Task Text', () => { 'The 9/11 Report', 'The 01/02 Report', 'a]7/8 debate', - '24/7 availability', ] cases.forEach(c => {