Add back regex support
Dan Abramov committed
Apr 4, 2019 at 17:05 UTC
2afda3de37e55dc89e1cecd63fcd3d9056f335a3
2 files changed
+18
-1
src/devtools/views/Components/SearchInput.js
+1
-1
@@ -95,7 +95,7 @@ export default function SearchInput(props: Props) {
95
onChange={handleTextChange}
96
onKeyDown={handleKeyDown}
97
onKeyPress={handleInputKeyPress}
98
- placeholder="Search"
98
+ placeholder="Search (text or /regex/)"
99
ref={inputRef}
100
value={searchText}
101
/>
src/devtools/views/utils.js
+17
@@ -6,6 +6,23 @@ import { meta } from '../../hydration';
6
import type { HooksTree } from 'src/backend/types';
7
8
export function createRegExp(string: string): RegExp {
9
+ // Allow /regex/ syntax with optional last /
10
+ if (string[0] === '/') {
11
+ // Cut off first slash
12
+ string = string.substring(1);
13
+ // Cut off last slash, but only if it's there
14
+ if (string[string.length - 1] === '/') {
15
+ string = string.substring(0, string.length - 1);
16
+ }
17
+ try {
18
+ return new RegExp(string, 'i');
19
+ } catch (err) {
20
+ // Bad regex. Make it not match anything.
21
+ // TODO: maybe warn in console?
22
+ return new RegExp('.^');
23
+ }
24
+ }
25
+
26
function isLetter(char: string) {
27
return char.toLowerCase() !== char.toUpperCase();
28
}