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

>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)

Leave a Reply