【发布时间】:2021-09-15 14:23:15
【问题描述】:
在我的打字稿文件中,我想使用包含条件表达式的正则表达式,但我的 IDE 强调了带有条件表达式报告的 RegEx 部分:“此正则表达式方言不支持条件”。如何启用支持条件正则表达式的方言?
export const phoneRegEx = /^\+?\d*\s?\(?(?(?<=\()\d+\)\s?\d+(-|\s|\.)?(?:\1?\d)*|(\.|\s|-)?(\2?\d)*)$/g;
我的 package.json 文件:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production"
},
"devDependencies": {
"@babel/preset-react": "^7.13.13",
"@tsconfig/create-react-app": "^1.0.2",
"@types/js-cookie": "^2.2.6",
"@types/react-router-dom": "^5.1.7",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"axios": "^0.21",
"eslint": "^7.28.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"file-loader": "^6.2.0",
"laravel-mix": "^6.0.6",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"ts-loader": "^9.2.3",
"typescript": "^4.3.2"
},
"dependencies": {
"@hookform/resolvers": "^2.5.2",
"@material-ui/core": "^4.11.4",
"@material-ui/icons": "^4.11.2",
"js-cookie": "^2.2.1",
"react-hook-form": "^7.8.3",
"react-paginate": "^7.1.3",
"react-router-dom": "^5.2.0",
"yup": "^0.32.9"
}
}
我的 tsconfig.json 文件:
{
"extends": "@tsconfig/create-react-app/tsconfig.json",
"compilerOptions": {
// "module": "CommonJS",
"target": "ES2020",
"sourceMap": true,
"jsx": "react",
"noEmit": false
},
"exclude": ["node_modules"],
"include": ["resources", "index.d.ts"]
}
我的 .eslintrc.json 文件:
{
"env": {
"browser": true,
"es2020": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
// "tsconfigRootDir": "./resources"
},
"plugins": [
"react",
"@typescript-eslint",
"react-hooks"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}
【问题讨论】:
-
你可以去掉条件并使用
/^\+?\d*\s?(?:\(\d+\)\s?\d+([-\s.]?)(?:\1?\d)*|([.\s-]?)(?:\2?\d)*)$/g -
@WiktorStribiżew 哇,它有效,很酷的解决方法
标签: javascript regex typescript eslint package.json