Home > OS >  javascript - how to format the phone number with dial code with regex
javascript - how to format the phone number with dial code with regex

Time:11-10

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

CodePudding user response:

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

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

CodePudding user response:

Here is a general match() approach:

var input = "(886) 7199 2483";
var output = " "   input.match(/\d /g).join(" ");
console.log(output);

  • Related