@@ -126,21 +126,37 @@ export const getDefaultJestCommand = (rootPath = ''): string | undefined => {
126
126
127
127
/**
128
128
* Escapes special characters in a string to be used as a regular expression pattern.
129
- * @param str - The string to escape.
130
- * @returns The escaped string.
129
+ * If the provided value is already a regex (indicated by `isRegExp`), it is returned unmodified.
131
130
*
132
- * Note: the conversion algorithm is taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
131
+ * [2025.04.14] Additionally, the function can replace actual newline characters with the literal sequence `\n`.
132
+ * This behavior is enabled by default (via `replaceNewLine = true`) to ensure that
133
+ * the regex pattern can be safely passed as a single command line argument—for example,
134
+ * for options like "--testPathPattern" or "testNamePattern". Even for regex usage, modifying
135
+ * newlines in this way can help prevent issues with multi-line arguments that might otherwise
136
+ * break in shell contexts.
137
+ *
138
+ * @param {string | StringPattern } str - The string or object to escape. When an object is provided,
139
+ * it must have a `value` property containing the string, and can optionally include:
140
+ * - `isRegExp`: if true, the function assumes the string is already a valid regex and returns it as-is.
141
+ * - `exactMatch`: if true, the resulting escaped string is anchored with '^' at the start and '$' at the end.
142
+ * @param {boolean } [replaceNewLine=true] - Whether to replace newline characters with the literal
143
+ * sequence "\n". This is beneficial when the result is used as a command line argument, even though
144
+ * it alters the actual newline characters that might be expected in some regex patterns.
145
+ *
146
+ * @returns {string } The escaped string, suitable for use as a regex pattern and for passing as a shell argument.
147
+ *
148
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
133
149
*/
134
- export function escapeRegExp ( str : string | StringPattern ) : string {
150
+ export function escapeRegExp ( str : string | StringPattern , replaceNewLine = true ) : string {
135
151
const sp : StringPattern = typeof str === 'string' ? { value : str } : str ;
152
+ let value : string ;
136
153
if ( sp . isRegExp ) {
137
- return sp . value ;
138
- }
139
- const escaped = sp . value . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ; // $& means the whole matched string
140
- if ( sp . exactMatch ) {
141
- return escaped + '$' ;
154
+ value = sp . value ;
155
+ } else {
156
+ const escaped = sp . value . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ; // $& means the whole matched string
157
+ value = sp . exactMatch ? escaped + '$' : escaped ;
142
158
}
143
- return escaped ;
159
+ return replaceNewLine ? value . replace ( / \r \n | \r | \n / g , '\\n' ) : value ;
144
160
}
145
161
146
162
/**
0 commit comments