Added function to escape regex characters

This commit is contained in:
Paweł Malak 2021-11-12 14:02:19 +01:00
parent 6281994be8
commit b848cfd921
3 changed files with 16 additions and 2 deletions

View file

@ -22,6 +22,9 @@ import { BookmarkGrid } from '../Bookmarks/BookmarkGrid/BookmarkGrid';
import { SearchBar } from '../SearchBar/SearchBar';
import { Header } from './Header/Header';
// Utils
import { escapeRegex } from '../../utility';
export const Home = (): JSX.Element => {
const {
apps: { apps, loading: appsLoading },
@ -60,7 +63,9 @@ export const Home = (): JSX.Element => {
if (localSearch) {
// Search through apps
setAppSearchResult([
...apps.filter(({ name }) => new RegExp(localSearch, 'i').test(name)),
...apps.filter(({ name }) =>
new RegExp(escapeRegex(localSearch), 'i').test(name)
),
]);
// Search through bookmarks
@ -70,7 +75,9 @@ export const Home = (): JSX.Element => {
category.bookmarks = categories
.map(({ bookmarks }) => bookmarks)
.flat()
.filter(({ name }) => new RegExp(localSearch, 'i').test(name));
.filter(({ name }) =>
new RegExp(escapeRegex(localSearch), 'i').test(name)
);
setBookmarkSearchResult([category]);
} else {

View file

@ -0,0 +1,6 @@
/**
* https://stackoverflow.com/a/30851002/16957052
*/
export const escapeRegex = (exp: string) => {
return exp.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
};

View file

@ -11,3 +11,4 @@ export * from './validators';
export * from './parseTime';
export * from './decodeToken';
export * from './applyAuth';
export * from './escapeRegex';