Skip to content

Commit dbaaf48

Browse files
authored
Merge pull request #1615 from devtron-labs/issue-resource-escape-search
fix: Issue in Search of resource browser with escape characters
2 parents b06acb1 + a141ea1 commit dbaaf48

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/components/common/helpers/Helpers.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { SIDEBAR_KEYS } from '../../ResourceBrowser/Constants'
1919
import { DEFAULT_SECRET_PLACEHOLDER } from '../../cluster/cluster.type'
2020
import { AUTO_SELECT } from '../../ClusterNodes/constants'
2121
import { ToastBody3 as UpdateToast } from '../ToastBody'
22+
import { PATTERNS } from '../../../config/constants'
2223

2324
export type IntersectionChangeHandler = (entry: IntersectionObserverEntry) => void
2425

@@ -1013,10 +1014,14 @@ export const highlightSearchedText = (searchText: string, matchString: string):
10131014
if (!searchText) {
10141015
return matchString
10151016
}
1016-
const highlightText = (highlighted) => `<mark>${highlighted}</mark>`
1017-
1018-
const regex = new RegExp(searchText, 'gi')
1019-
return matchString.replace(regex, highlightText)
1017+
try {
1018+
const highlightText = (highlighted) => `<mark>${highlighted}</mark>`
1019+
const escapedSearchText = searchText.replace(PATTERNS.ESCAPED_CHARACTERS, '\\$&') // Escape special characters handling
1020+
const regex = new RegExp(escapedSearchText, 'gi')
1021+
return matchString.replace(regex, highlightText)
1022+
} catch (err) {
1023+
return matchString
1024+
}
10201025
}
10211026

10221027
export const trackByGAEvent = (category: string, action: string): void => {

src/config/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export const PATTERNS = {
291291
ALPHANUMERIC_WITH_SPECIAL_CHAR: /^[A-Za-z0-9._-]+$/, // allow alphanumeric,(.) ,(-),(_)
292292
CUSTOM_TAG: /^(?![.-])([a-zA-Z0-9_.-]*\{[Xx]\}[a-zA-Z0-9_.-]*)(?<![.-])$/, //Allowed: Alphanumeric characters, including (_) (.) (-) {x} {X} but cannot begin or end with (.) or (-)
293293
ALPHANUMERIC_WITH_SPECIAL_CHAR_AND_SLASH: /^[A-Za-z0-9._/-]+$/, // allow alphanumeric,(.) ,(-),(_),(/)
294+
ESCAPED_CHARACTERS: /[.*+?^${}()|[\]\\]/g,
294295
}
295296

296297
export const TriggerType = {

0 commit comments

Comments
 (0)