Conditionals are not supported in this regex dialect

Viewed 253

In my typescript file I want to use regular expression that includes conditional expression but my IDE underscores the part of RegEx with conditional expression reporting:"Conditionals are not supported in this regex dialect". How can I enable dialect that supports conditional regular expressions?

export const phoneRegEx = /^\+?\d*\s?\(?(?(?<=\()\d+\)\s?\d+(-|\s|\.)?(?:\1?\d)*|(\.|\s|-)?(\2?\d)*)$/g;

my package.json file:

{
    "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"
    }
}

my tsconfig.json file:

{
    "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"]
}

my .eslintrc.json file:

{
    "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"
    }
}
1 Answers

The problem is that JavaScript regex does not support conditional constructs, seee (?(?<=\()...|...) in your regex. It means that the first alternative will be matched only if there is a ( char immediately before it, else, the second alternative will be tried.

I suggest getting rid of the conditional completely by moving \( pattern to the first alternative of the subsequent group (while removing the ? from the pattern to make it obligatory):

/^\+?\d*\s?(?:\(\d+\)\s?\d+([-\s.]?)(?:\1?\d)*|([.\s-]?)(?:\2?\d)*)$/g
//            ^^

See the regex demo. Details:

  • ^ - start of string
  • \+? - an optional +
  • \d* - zero or more digits
  • \s? - an optional whitespace
  • (?: - start of a non-capturing group:
    • \(\d+\) - (, one or more digits, )
    • \s? - an optional whitespace
    • \d+ - one or more digits
    • ([-\s.]?) - Group 1: an optional -, whitespace or .
    • (?:\1?\d)* - zero or more occurrences of the optional Group 1 value followed with any one digit
    • | - or
    • ([.\s-]?) - Group 2: an optional -, whitespace or .
    • (?:\2?\d)* - zero or more occurrences of the optional Group 2 value followed with any one digit
  • ) - end of the group
  • $ - end of string.
Related