Skip to content

Commit 365764a

Browse files
committed
Cascader: escape special characters for regexp
1 parent 8de71d0 commit 365764a

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

packages/cascader/src/main.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import emitter from 'element-ui/src/mixins/emitter';
7070
import Locale from 'element-ui/src/mixins/locale';
7171
import { t } from 'element-ui/src/locale';
7272
import debounce from 'throttle-debounce/debounce';
73-
import { generateId } from 'element-ui/src/utils/util';
73+
import { generateId, escapeRegexpString } from 'element-ui/src/utils/util';
7474
7575
const popperMixin = {
7676
props: {
@@ -337,7 +337,8 @@ export default {
337337
}
338338
339339
let filteredFlatOptions = flatOptions.filter(optionsStack => {
340-
return optionsStack.some(option => new RegExp(value, 'i').test(option[this.labelKey]));
340+
return optionsStack.some(option => new RegExp(escapeRegexpString(value), 'i')
341+
.test(option[this.labelKey]));
341342
});
342343
343344
if (filteredFlatOptions.length > 0) {

packages/select/src/option.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<script type="text/babel">
1919
import Emitter from 'element-ui/src/mixins/emitter';
20-
import { getValueByPath } from 'element-ui/src/utils/util';
20+
import { getValueByPath, escapeRegexpString } from 'element-ui/src/utils/util';
2121
2222
export default {
2323
mixins: [Emitter],
@@ -129,9 +129,7 @@
129129
},
130130
131131
queryChange(query) {
132-
// query 里如果有正则中的特殊字符,需要先将这些字符转义
133-
let parsedQuery = String(query).replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
134-
this.visible = new RegExp(parsedQuery, 'i').test(this.currentLabel) || this.created;
132+
this.visible = new RegExp(escapeRegexpString(query), 'i').test(this.currentLabel) || this.created;
135133
if (!this.visible) {
136134
this.select.filteredOptionsCount--;
137135
}

src/utils/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ export const valueEquals = (a, b) => {
8282
}
8383
return true;
8484
};
85+
86+
export const escapeRegexpString = value => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');

0 commit comments

Comments
 (0)