Regular Expression to reformat a US phone number in Javascript

后端 未结 13 2147
眼角桃花
眼角桃花 2020-12-02 07:32

I\'m looking to reformat (replace, not validate - there are many references for validating) a phone number for display in Javascript. Here\'s an example of some of the data:

相关标签:
13条回答
  • 2020-12-02 08:27

    I've extended David Baucum's answer to include support for extensions up to 4 digits in length. It also includes the parentheses requested in the original question. This formatting will work as you type in the field.

    phone = phone.replace(/\D/g, '');
    const match = phone.match(/^(\d{1,3})(\d{0,3})(\d{0,4})(\d{0,4})$/);
    if (match) {
        phone = `(${match[1]}${match[2] ? ') ' : ''}${match[2]}${match[3] ? '-' : ''}${match[3]}${match[4] ? ' x' : ''}${match[4]}`;
    }
    return phone;
    
    0 讨论(0)
  • 2020-12-02 08:32

    For US Phone Numbers

    /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/
    

    Let’s divide this regular expression in smaller fragments to make is easy to understand.

    • /^\(?: Means that the phone number may begin with an optional (.
    • (\d{3}): After the optional ( there must be 3 numeric digits. If the phone number does not have a (, it must start with 3 digits. E.g. (308 or 308.
    • \)?: Means that the phone number can have an optional ) after first 3 digits.
    • [- ]?: Next the phone number can have an optional hyphen (-) after ) if present or after first 3 digits.
    • (\d{3}): Then there must be 3 more numeric digits. E.g (308)-135 or 308-135 or 308135
    • [- ]?: After the second set of 3 digits the phone number can have another optional hyphen (-). E.g (308)-135- or 308-135- or 308135-
    • (\d{4})$/: Finally, the phone number must end with four digits. E.g (308)-135-7895 or 308-135-7895 or 308135-7895 or 3081357895.

      Reference :

    http://www.zparacha.com/phone_number_regex/

    0 讨论(0)
  • 2020-12-02 08:35

    This answer borrows from maerics' answer. It differs primarily in that it accepts partially entered phone numbers and formats the parts that have been entered.

    phone = value.replace(/\D/g, '');
    const match = phone.match(/^(\d{1,3})(\d{0,3})(\d{0,4})$/);
    if (match) {
      phone = `${match[1]}${match[2] ? ' ' : ''}${match[2]}${match[3] ? '-' : ''}${match[3]}`;
    }
    return phone
    
    0 讨论(0)
  • 2020-12-02 08:35
    var numbers = "(123) 456-7890".replace(/[^\d]/g, ""); //This strips all characters that aren't digits
    if (numbers.length != 10) //wrong format
        //handle error
    var phone = "(" + numbers.substr(0, 3) + ") " + numbers.substr(3, 3) + "-" + numbers.substr(6); //Create format with substrings
    
    0 讨论(0)
  • 2020-12-02 08:36

    var x = '301.474.4062';
        
    x = x.replace(/\D+/g, '')
         .replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
    
    alert(x);

    0 讨论(0)
  • 2020-12-02 08:37

    You can use this functions to check valid phone numbers and normalize them:

    let formatPhone = (dirtyNumber) => {
     return dirtyNumber.replace(/\D+/g, '').replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
    }
    
    let isPhone = (phone) => {
       //normalize string and remove all unnecessary characters
       phone = phone.replace(/\D+/g, '');
       return phone.length == 10? true : false;
    }
    
    0 讨论(0)
提交回复
热议问题