安装依赖
yarn add regexpu-core
yarn add @babel/plugin-proposal-unicode-property-regex --dev
babel.config.js
module.exports = {
presets: [
'@vue/app',
],
plugins: [
[
'@babel/plugin-proposal-unicode-property-regex',
{
useUnicodeFlag: true,
},
],
],
};
tools.js
const rewritePattern = require('regexpu-core');
const _regexpu = rewritePattern('\\p{Unified_Ideograph}', 'u', {
'unicodePropertyEscape': true,
'useUnicodeFlag': false,
})
const regex = new RegExp(_regexpu, 'u');
if (regex.test(item)) {
// 验证通过
}