From 2afda3de37e55dc89e1cecd63fcd3d9056f335a3 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 4 Apr 2019 17:05:34 +0100 Subject: [PATCH] Add back regex support --- src/devtools/views/Components/SearchInput.js | 2 +- src/devtools/views/utils.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/devtools/views/Components/SearchInput.js b/src/devtools/views/Components/SearchInput.js index 9317f2d476..51d0e58d3b 100644 --- a/src/devtools/views/Components/SearchInput.js +++ b/src/devtools/views/Components/SearchInput.js @@ -95,7 +95,7 @@ export default function SearchInput(props: Props) { onChange={handleTextChange} onKeyDown={handleKeyDown} onKeyPress={handleInputKeyPress} - placeholder="Search" + placeholder="Search (text or /regex/)" ref={inputRef} value={searchText} /> diff --git a/src/devtools/views/utils.js b/src/devtools/views/utils.js index 2e54854982..2db6fb82a7 100644 --- a/src/devtools/views/utils.js +++ b/src/devtools/views/utils.js @@ -6,6 +6,23 @@ import { meta } from '../../hydration'; import type { HooksTree } from 'src/backend/types'; export function createRegExp(string: string): RegExp { + // Allow /regex/ syntax with optional last / + if (string[0] === '/') { + // Cut off first slash + string = string.substring(1); + // Cut off last slash, but only if it's there + if (string[string.length - 1] === '/') { + string = string.substring(0, string.length - 1); + } + try { + return new RegExp(string, 'i'); + } catch (err) { + // Bad regex. Make it not match anything. + // TODO: maybe warn in console? + return new RegExp('.^'); + } + } + function isLetter(char: string) { return char.toLowerCase() !== char.toUpperCase(); }