Match search at word boundaries
Dan Abramov committed
Apr 4, 2019 at 16:22 UTC
8cfb0e20c7038abe0fa4b46bdda171b31d2b1855
1 file changed
+28
-1
src/devtools/views/utils.js
+28
-1
@@ -6,7 +6,34 @@ import { meta } from '../../hydration';
6
import type { HooksTree } from 'src/backend/types';
7
8
export function createRegExp(string: string): RegExp {
9
- return new RegExp(escapeStringRegExp(string), 'i');
9
+ // 'item' should match 'Item' and 'ListItem', but not 'InviteMom'.
10
+ // To do this, we'll slice off 'tem' and check first letter separately.
11
+ const escaped = escapeStringRegExp(string);
12
+ const firstLetter = escaped[0];
13
+ let restRegex = '';
14
+ // For 'item' input, restRegex becomes '[tT][eE][mM]'
15
+ // We can't simply make it case-insensitive because first letter case matters.
16
+ for (let i = 1; i < escaped.length; i++) {
17
+ const char = escaped[i];
18
+ restRegex += '[' + char.toLowerCase() + char.toUpperCase() + ']';
19
+ }
20
+ // Respect first letter only if it starts a word.
21
+ return new RegExp(
22
+ // For example:
23
+ // (^[iI]|I)[tT][eE][mM]
24
+ // Matches:
25
+ // 'Item'
26
+ // 'ListItem'
27
+ // but not 'InviteMom'
28
+ '(^[' +
29
+ firstLetter +
30
+ firstLetter.toLowerCase() +
31
+ ']' +
32
+ '|' +
33
+ firstLetter.toUpperCase() +
34
+ ')' +
35
+ restRegex
36
+ );
37
}
38
39
export function getMetaValueLabel(data: Object): string | null {