This is not a duplicate, the linked thread does not explain how to achieve this.
I'm looking to get a phone number in a specific format.
+xx (x) xxx xxx xxxx
- Country code.
- Space.
- Zero in brackets.
- Space.
- 3 digits.
- Space.
- 3 digits.
- Space.
- 4 digits.
The user could type anything in (but should always be a +61 number). So far I have tried the below.
- Removing spaces and non numeric characters.
- If starting with a zero, remove.
- If starting with 610, remove.
- If starting with 61, remove.
- Re add country code in specific format and format rest of phone number is a 3,3,4 format.
My question, is - is there a way to simply the below to perhaps one expression?
value = value.replace(/\D/g,'');
value = value.startsWith(0) ? value.substring(1) : value;
value = value.startsWith('610') ? value.substring(3) : value;
value = value.startsWith('61') ? value.substring(2) : value;
value = '+61 (0) ' + value.replace(/\d{3,4}?(?=...)/g, '$& ');