diff --git a/src/mol-script/transpilers/helper.ts b/src/mol-script/transpilers/helper.ts index 03b3d583aedf184e59595d37f5361b9b72c4167b..6a245626b05c14ff6dfe65cbdfb0019aa071e822 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 73604b6933872173944d72344de79d46031a6368..1db2675730b0e3ee81bc18983ba0be8ef9265b44 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