File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -101,3 +101,20 @@ export function isMatchingPattern(value: string, pattern: RegExp | string): bool
101
101
}
102
102
return false ;
103
103
}
104
+
105
+ /**
106
+ * Given a string, escape characters which have meaning in the regex grammar, such that the result is safe to feed to
107
+ * `new RegExp()`.
108
+ *
109
+ * Based on https://github.com/sindresorhus/escape-string-regexp. Vendored to a) reduce the size by skipping the runtime
110
+ * type-checking, and b) ensure it gets down-compiled for old versions of Node (the published package only supports Node
111
+ * 12+).
112
+ *
113
+ * @param regexString The string to escape
114
+ * @returns An version of the string with all special regex characters escaped
115
+ */
116
+ export function escapeStringForRegex ( regexString : string ) : string {
117
+ // escape the hyphen separately so we can also replace it with a unicode literal hyphen, to avoid the problems
118
+ // discussed in https://github.com/sindresorhus/escape-string-regexp/issues/20.
119
+ return regexString . replace ( / [ | \\ { } ( ) [ \] ^ $ + * ? . ] / g, '\\$&' ) . replace ( / - / g, '\\x2d' ) ;
120
+ }
You can’t perform that action at this time.
0 commit comments