From c39b3569ded143781eb45c01a07ae054f75fd809 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alexander.rose@weirdbyte.de> Date: Wed, 21 Dec 2022 20:11:14 -0800 Subject: [PATCH] add escapeRegExp util --- src/mol-script/transpilers/helper.ts | 5 ++--- src/mol-util/string.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mol-script/transpilers/helper.ts b/src/mol-script/transpilers/helper.ts index 03b3d583a..6a245626b 100644 --- a/src/mol-script/transpilers/helper.ts +++ b/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. diff --git a/src/mol-util/string.ts b/src/mol-util/string.ts index 73604b693..1db267573 100644 --- a/src/mol-util/string.ts +++ b/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 } \ No newline at end of file -- GitLab