2

Trying to understand what the below regex means.

/^[0-9]{2,3}[- ]{0,1}[0-9]{3}[- ]{0,1}[0-9]{3}$/

Sorry not exactly a coding question.

Ela Buwa
  • 1,467
  • 2
  • 20
  • 34

2 Answers2

2

Let's break this regex into a few different parts:

  • ^: asserts position at start of the string
  • [0-9]{2,3}: Match a number between 0 and 9, between 2 and 3 times
  • [- ]{0,1} Matches a dash between zero and one times (Optional dash)
  • [0-9]{3}: Match a number between 0 and 9, exactly 3 times
  • [- ]{0,1} Matches a dash between zero and one times (Optional dash)
  • [0-9]{3}: Match a number between 0 and 9, exactly 3 times
  • $: asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Here are a few strings that would pass this regex:

  • 123-123-123
  • 123123123
  • 12-123-123
  • 12123123

Here's a good resource to learn/test regexes: regex101.com

Zubair
  • 626
  • 1
  • 7
  • 14
1

It matches two or three digits followed by (optionally) a dash or space, then 3 digits, again optional dash or space and 3 digits. It seems to try to match a telephone number written in different formats.

jacalvo
  • 606
  • 5
  • 14