Browse Source

Extract RegExp escaping into helper function

Danilo Bargen 7 năm trước cách đây
mục cha
commit
e2767173c6
2 tập tin đã thay đổi với 13 bổ sung3 xóa
  1. 4 3
      src/filters.ts
  2. 9 0
      src/helpers.ts

+ 4 - 3
src/filters.ts

@@ -15,7 +15,7 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
-import {filter} from './helpers';
+import {escapeRegExp, filter} from './helpers';
 import {MimeService} from './services/mime';
 import {WebClientService} from './services/webclient';
 
@@ -152,8 +152,9 @@ angular.module('3ema.filters', [])
 
                         if (html !== undefined) {
                             text = text.replace(
-                                new RegExp(possibleMention.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'), 'g'),
-                                html);
+                                new RegExp(escapeRegExp(possibleMention), 'g'),
+                                html,
+                            );
                         }
                     }
                 }

+ 9 - 0
src/helpers.ts

@@ -242,3 +242,12 @@ export function supportsPassive() {
         window.addEventListener('test', null, opts);
     } catch (e) { /* do nothing */ }
 }
+
+/**
+ * Excape a RegEx, so that none of the string characters are considered special characters.
+ *
+ * Taken from https://stackoverflow.com/a/17606289/284318
+ */
+export function escapeRegExp(str: string) {
+    return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
+}