Skip to content

Commit

Permalink
Added function to escape regex characters
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelmalak committed Nov 12, 2021
1 parent 6281994 commit b848cfd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
11 changes: 9 additions & 2 deletions client/src/components/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions client/src/utility/escapeRegex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* https://stackoverflow.com/a/30851002/16957052
*/
export const escapeRegex = (exp: string) => {
return exp.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
};
1 change: 1 addition & 0 deletions client/src/utility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './validators';
export * from './parseTime';
export * from './decodeToken';
export * from './applyAuth';
export * from './escapeRegex';

0 comments on commit b848cfd

Please sign in to comment.