mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Match search at word boundaries
This commit is contained in:
@@ -6,7 +6,34 @@ import { meta } from '../../hydration';
|
||||
import type { HooksTree } from 'src/backend/types';
|
||||
|
||||
export function createRegExp(string: string): RegExp {
|
||||
return new RegExp(escapeStringRegExp(string), 'i');
|
||||
// 'item' should match 'Item' and 'ListItem', but not 'InviteMom'.
|
||||
// To do this, we'll slice off 'tem' and check first letter separately.
|
||||
const escaped = escapeStringRegExp(string);
|
||||
const firstLetter = escaped[0];
|
||||
let restRegex = '';
|
||||
// For 'item' input, restRegex becomes '[tT][eE][mM]'
|
||||
// We can't simply make it case-insensitive because first letter case matters.
|
||||
for (let i = 1; i < escaped.length; i++) {
|
||||
const char = escaped[i];
|
||||
restRegex += '[' + char.toLowerCase() + char.toUpperCase() + ']';
|
||||
}
|
||||
// Respect first letter only if it starts a word.
|
||||
return new RegExp(
|
||||
// For example:
|
||||
// (^[iI]|I)[tT][eE][mM]
|
||||
// Matches:
|
||||
// 'Item'
|
||||
// 'ListItem'
|
||||
// but not 'InviteMom'
|
||||
'(^[' +
|
||||
firstLetter +
|
||||
firstLetter.toLowerCase() +
|
||||
']' +
|
||||
'|' +
|
||||
firstLetter.toUpperCase() +
|
||||
')' +
|
||||
restRegex
|
||||
);
|
||||
}
|
||||
|
||||
export function getMetaValueLabel(data: Object): string | null {
|
||||
|
||||
Reference in New Issue
Block a user