Skip to content

Commit 85660f8

Browse files
committed
escape json value
1 parent 8deceee commit 85660f8

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,21 @@ function MultiSelect({
7575

7676
for (const value of defaultValArr) {
7777
const matchedVals = optionString.match(
78-
new RegExp(`{+?.[^{]*?"${value}"}`, 'g')
78+
new RegExp(
79+
`{+?.[^{]*?"${value
80+
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') // escape special chars
81+
.replace(/"/g, '\\\\$&')}"}`, // escape " as \\
82+
'g'
83+
)
7984
)
85+
8086
if (matchedVals) {
8187
if (matchedVals.length === 1) {
8288
defaultValueObj.push(JSON.parse(matchedVals))
8389
} else if (matchedVals.length > 1) {
8490
defaultValueObj.push(JSON.parse(`[${matchedVals.join(',')}]`))
8591
}
86-
} else if (value !== '' && value !== null) {
92+
} else if (value !== '' && value !== null && customValue) {
8793
setExtraValue(value)
8894
defaultValueObj.push({ label: value, value })
8995
}
@@ -259,7 +265,6 @@ function MultiSelect({
259265
tmp = tmp.filter((itm) => itm.value !== newValObj.value)
260266
}
261267
}
262-
263268
setNewValue(tmp)
264269
setSearch(null)
265270
document.querySelector('.msl-input').innerHTML = ''

0 commit comments

Comments
 (0)