- 需求:需要根据输入的字符串对数据源进行筛选,兼容全角半角
import _, { escapeRegExp } from 'lodash';
const halfWidthToFullWidth = (str) => {
return str?.replace(/[\x21-\x7E]/g, (c) => {
return String.fromCharCode(c.charCodeAt(0) + 0xfee0)
})
};
const isMatchStr = (searchValue, dataSourceValue) => {
if(searchValue && dataSourceValue){
const regex = new RegExp(escapeRegExp(halfWidthToFullWidth(searchValue)));
return halfWidthToFullWidth(dataSourceValue).match(regex);
}
return false;
};
isMatchStr('芜湖(呜呼)', '芜湖(呜呼)')