Formatting the phone number

前端 未结 3 432
离开以前
离开以前 2020-12-12 00:13

The below textbox is printing the value of the phone number after getting it from the database.

3条回答
  •  醉梦人生
    2020-12-12 00:33

    const formatPhoneNumber = (phoneNumber) => {
      // convert the raw number to (xxx) xxx-xxx format
      const x = phoneNumber && phoneNumber.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
      return !x[2] ? x[1] : `(${x[1]}) ${x[2]}${x[3] ? `-${x[3]}` : ''}`;
    };
    
    console.log(formatPhoneNumber("1111111111"));

提交回复
热议问题