Looking to format a phone number string by adding an extension? Consider alternatives to inserting at each index. For example, if the input is 1234567891346
, the desired output would be (123) - 456 - 7891 Ext - 346
.
let phoneStr = '12345678912346';
let phoneNo = [ '(' , phoneStr[0], phoneStr[1], phoneStr[2], ')', ' - ',
phoneStr[3], phoneStr[4], phoneStr[5], ' - ',
phoneStr[6], phoneStr[7], phoneStr[8], phoneStr[9], ' Ext - ',
phoneStr[10], phoneStr[11], phoneStr[12], phoneStr[13], phoneStr[14]];
console.log(phoneNo.join("").replace(/,/g , ""));