Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript – how to format the phone number with dial code with regex

How to make (886) 7199 2483 to +886 7199 2483 using regex?

I can only extract the dial code by

const dialCode = '(886) 7199 2483'.match(/\(([^)]+)\)/)[1]; // 886

but don’t know how to do the next step

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use group match to do it,regex demo

let str = `(886) 7199 2483`
str = str.replace(/\((\d+)\)/, "+$1")
console.log(str)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading