I have a phone number (phone) which will be sent to an API endpoint via a PUT request. Before sending to the API, I want to format it.
Cases:
1)
Phone number: 0321 1234567
Send to API like: +3553211234567
Phone Number: +355 321 1234567
Send to API like: +3553211234567
Phone Number: +3553211234567
Send to API like: +3553211234567
So, what I need to do is:
1- remove empty spaces
for this I'm using:
phone.replace(/[^+\d]+/g, "")
remove everything except + (for country codes) and digits.
2- check if the phone number starts with 0. If true, replace it with +355.
How can I do this in Javascript? Thank you in advance.