@@ -561,15 +561,18 @@ function _valueAsString(value: unknown) {
561
561
if ( value === undefined ) {
562
562
return 'undefined' ;
563
563
}
564
- // `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer.
565
564
try {
566
- return JSON . stringify ( value , ( _ , v ) => {
567
- if ( v instanceof RegExp ) {
568
- return `/${ v . toString ( ) } /` ;
569
- }
570
-
571
- return typeof v === 'string' ? v . replace ( '/\//g' , '\\/' ) : v ;
572
- } ) . replace ( / " \/ \/ / g, '\\/' ) . replace ( / \/ \/ " / g, '\\/' ) . replace ( / \\ \/ / g, '/' ) ;
565
+ // `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer.
566
+ // Use a character that is unlikely to appear in real strings to denote the start and end of
567
+ // the regex. This allows us to strip out the extra quotes around the value added by
568
+ // `JSON.stringify`. Also do custom escaping on `"` characters to prevent `JSON.stringify`
569
+ // from escaping them as if they were part of a string.
570
+ const stringifiedValue = JSON . stringify ( value , ( _ , v ) => v instanceof RegExp ?
571
+ `◬MAT_RE_ESCAPE◬${ v . toString ( ) . replace ( / " / g, '◬MAT_RE_ESCAPE◬' ) } ◬MAT_RE_ESCAPE◬` : v ) ;
572
+ // Strip out the extra quotes around regexes and put back the manually escaped `"` characters.
573
+ return stringifiedValue
574
+ . replace ( / " ◬ M A T _ R E _ E S C A P E ◬ | ◬ M A T _ R E _ E S C A P E ◬ " / g, '' )
575
+ . replace ( / ◬ M A T _ R E _ E S C A P E ◬ / g, '"' ) ;
573
576
} catch {
574
577
// `JSON.stringify` will throw if the object is cyclical,
575
578
// in this case the best we can do is report the value as `{...}`.
0 commit comments