Oljebra
Oljebra

Reputation: 3

How to fix regex Unterminated regular expression literal

I currently have this regex string and i am getting a Unterminated regular expression literal error in my react code

const check_number = /^[0]\d{10}$)|(^[\+]?[234]\d{12}$

does anyone know how to fix this.

Upvotes: 0

Views: 4014

Answers (2)

Alexander Nied
Alexander Nied

Reputation: 13623

The problem is you have an invalid regular expression, for a number of reasons:

  1. You are missing a closing / character at the end
  2. You have a closing parenthesis in the start with no opening parenthesis
  3. You have an opening parenthesis in the middle with no closing paren

Upvotes: -1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

The exact syntax error is being caused by a missing / ending delimiter (and a few other things). But, your regex has other problems. Use this version:

const check_number = /^(?:0\d{10}|[+]?[234]\d{12})$/

The above pattern matches phone numbers which either:

  • Start with zero and have 11 total digits, or
  • Start with an optional +, followed by 2, 3, or 4, and have 13 total digits

Upvotes: 1

Related Questions