Skip to content

Commit c7e494b

Browse files
authored
[React DevTools] Fix regex for formateWithStyles function (#24486)
The previous regex to detect string substitutions is not quite right, this PR fixes it by: Check to make sure we are starting either at the beginning of the line or we match a character that's not % to make sure we capture all the % in a row. Make sure there are an odd number of % (the first X pairs are escaped % characters. The odd % followed by a letter is the string substitution)
1 parent 6cbf0f7 commit c7e494b

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

packages/react-devtools-shared/src/backend/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/**
12
/**
23
* Copyright (c) Facebook, Inc. and its affiliates.
34
*
@@ -188,7 +189,7 @@ export function formatWithStyles(
188189
}
189190

190191
// Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
191-
const REGEXP = /([^%]|^)(%([oOdisf]))/g;
192+
const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
192193
if (inputArgs[0].match(REGEXP)) {
193194
return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
194195
} else {

packages/react-devtools-shared/src/hook.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function installHook(target: any): DevToolsHook | null {
189189
}
190190

191191
// Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
192-
const REGEXP = /([^%]|^)(%([oOdisf]))/g;
192+
const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
193193
if (inputArgs[0].match(REGEXP)) {
194194
return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
195195
} else {

0 commit comments

Comments
 (0)