refactor(utils): improve escapeStringRegexp (#6013)

This commit is contained in:
三咲智子
2022-02-13 22:48:42 +08:00
committed by GitHub
parent cd8a053b3c
commit 7abc200742
2 changed files with 7 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
import { inject, computed, getCurrentInstance, watch, toRaw, unref } from 'vue'
import { get } from 'lodash-unified'
import { escapeRegexpString } from '@element-plus/utils'
import { escapeStringRegexp } from '@element-plus/utils'
import { selectKey, selectGroupKey } from './token'
import type { Ref } from 'vue'
@@ -120,7 +120,7 @@ export function useOption(props, states) {
watch(queryChange, (changes: Ref<QueryChangeCtx>) => {
const { query } = unref(changes)
const regexp = new RegExp(escapeRegexpString(query), 'i')
const regexp = new RegExp(escapeStringRegexp(query), 'i')
states.visible = regexp.test(currentLabel.value) || props.created
if (!states.visible) {
select.filteredOptionsCount--

View File

@@ -5,5 +5,8 @@ export {
hyphenate as kebabCase, // alias
} from '@vue/shared'
export const escapeRegexpString = (value = '') =>
String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
/**
* fork from {@link https://github.com/sindresorhus/escape-string-regexp}
*/
export const escapeStringRegexp = (string = '') =>
string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')