0

I'm catching international numbers and running a regex to replace the characters people like to put between numbers.

I'm using the below RegEx:

[+]([0-9]{1,3})(([\s\-\.\(\)]*)([0-9]*)([\s\-\.\(\)]*)){1,3}

It works great but when I use a repeated group, it only catches the last iteration. When I use the regex101 site to debug my regular expression, I see:

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations

I want to take the advice but I'm not sure how I can put a capturing group around the repeated group. See: https://regex101.com/r/pT3cK9/1

Err
  • 890
  • 2
  • 11
  • 32

1 Answers1

0

As stated in comments, the simples way to clean phone numbers would be to define a list of unwanted characters, and replace them by spaces:

'+94 (666) 999-5555'.replace(/[ .()-]+/g, ' '); // +94 666 999 5555
'+42 555.123.4567'.replace(/[ .()-]+/g, ' ');   // +42 555 123 4567
zessx
  • 68,042
  • 28
  • 135
  • 158