From 1f66fae65ec495ecb369808359e4610127683704 Mon Sep 17 00:00:00 2001
From: jojohoch <joachim.hoch@iqb.hu-berlin.de>
Date: Fri, 11 Oct 2024 13:34:16 +0200
Subject: [PATCH] Improve regular expressions for defining markables

---
 projects/player/src/app/classes/markable-support.ts | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/projects/player/src/app/classes/markable-support.ts b/projects/player/src/app/classes/markable-support.ts
index 155dd2c18..4a8c02c4e 100644
--- a/projects/player/src/app/classes/markable-support.ts
+++ b/projects/player/src/app/classes/markable-support.ts
@@ -10,6 +10,11 @@ export class MarkableSupport {
   private applicationRef: ApplicationRef;
   markables: Markable[] = [];
 
+  static wordsWithWhitespace: RegExp = /(\s*\S+\s*)|(s+\S*\s*)|(s*\S*\s+)/g;
+  static prefix: RegExp = /\W+(?=\w+)/u;
+  static word: RegExp = /\w+/u;
+  static suffix: RegExp = /\W+$/u;
+
   constructor(
     renderer: Renderer2,
     applicationRef: ApplicationRef
@@ -70,11 +75,11 @@ export class MarkableSupport {
 
   private static getMarkables(text: string, startIndex: number, savedMarks: string[]): Markable[] {
     const markables: Markable[] = [];
-    const wordsWithWhitespace = text?.match(/(\s*\S+\s*)|(s+\S*\s*)|(s*\S*\s+)/g);
+    const wordsWithWhitespace = text?.match(MarkableSupport.wordsWithWhitespace);
     wordsWithWhitespace?.forEach((wordWithWhitespace: string, index: number) => {
-      const prefix = wordWithWhitespace.match(/\s+(?=[^,]*\S*)/);
-      const word = wordWithWhitespace.match(/\S+/);
-      const suffix = wordWithWhitespace.match(/[^\S]\s*$/);
+      const prefix = wordWithWhitespace.match(MarkableSupport.prefix);
+      const word = wordWithWhitespace.match(MarkableSupport.word);
+      const suffix = wordWithWhitespace.match(MarkableSupport.suffix);
       const id = startIndex + index;
       const color = MarkableSupport.getColorValueById(id, savedMarks);
       markables.push(
-- 
GitLab