Jelajahi Sumber

add escapeRegExp util

Alexander Rose 2 tahun lalu
induk
melakukan
c39b3569de
2 mengubah file dengan 11 tambahan dan 3 penghapusan
  1. 2 3
      src/mol-script/transpilers/helper.ts
  2. 9 0
      src/mol-util/string.ts

+ 2 - 3
src/mol-script/transpilers/helper.ts

@@ -13,10 +13,9 @@ import { MolScriptBuilder } from '../../mol-script/language/builder';
 const B = MolScriptBuilder;
 import { Expression } from '../language/expression';
 import { KeywordDict, PropertyDict, FunctionDict, OperatorList } from './types';
+import { escapeRegExp } from '../../mol-util/string';
 
-export function escapeRegExp(s: String) {
-    return String(s).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
-}
+export { escapeRegExp };
 
 // Takes a parser for the prefix operator, and a parser for the base thing being
 // parsed, and parses as many occurrences as possible of the prefix operator.

+ 9 - 0
src/mol-util/string.ts

@@ -93,4 +93,13 @@ export function trimCharEnd(str: string, char: string) {
 /** Simple function to strip tags from a string */
 export function stripTags(str: string) {
     return str.replace(/<\/?[^>]+>/g, '');
+}
+
+/**
+ * Escape string for use in Javascript regex
+ *
+ * From https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex/6969486#6969486
+ */
+export function escapeRegExp(str: string) {
+    return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
 }