1

Is there one regex expression I can use to produce the phone number formatting as below. Or do I need more expressions/coding to handle all the below.

2159018060    -> 215-901-8060
(215) 901 8060-> 215-901-8060
(215)-901-8060-> 215-901-8060
1.215.901.8060 -> 215-901-8060
+1-215-9018060 -> 215-901-8060
+1-215-901-8060 x233 -> 215-901-8060 x233
+1-215-901-8060 Ext 233 -> 215-901-8060 Ext 233
+44 20 7323 8299 -> +44 20 7323 8299

I have the below one that handles most of it.

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

But not sure how to handle the start with +1

Thanks Thanks

4
  • regex101.com/r/dI1vS9/1 seems to cover all your cases, but I doubt that it can cover any occuring case. Commented May 25, 2016 at 21:17
  • I believe it is for the North American phone numbers only, right? Commented May 25, 2016 at 21:23
  • The difference in the result when input starts with +1 or +44 seems meaningless to me. Commented May 25, 2016 at 22:03
  • regex101.com/r/dI1vS9/1 seem to work great. Thank you Commented May 26, 2016 at 14:53

1 Answer 1

3

This first clears out any non-digit characters. This (theoretically) should include spaces, so if yours has spaces you may need alter it just a little.

The next bit of regex searches the string for a match that has 3 groups of numbers of length 3, 3, and 4.

note to get it to work, type in a phone number and then tab off of it.

jQuery included to make life a little faster.

$(document).ready(function(){
  $('#phone').change(function(){
    var value = $(this).val();
    
    value = value.replace(/[^\d]/g,'');
    value = value.replace(/^([0-9]{3})([0-9]{3})([0-9]{4})$/,'($1) $2-$3');
    $('#results').html(value);
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="phone"/>
<div id="results"></div>

edit

Took me a bit, but this should work for your question. let me know if anything doesn't work.

var nums_to_try = [
  '2159018060',
  '(215) 901 8060',
  '(215)-901-8060',
  '1.215.901.8060',
  '+1-215-9018060',
  '+1-215-901-8060 x233',
  '+1-215-901-8060 Ext 233',
  '+44 20 7323 8299'
]; //quick test to make sure all demonstrated cases are input

$(document).ready(function() {

  var numbers = [{
    name: 'phone10', //property was mostly used for debug. you can probably remove this without any errors
    pattern: /^([0-9]{3})\-?([0-9]{3})\-?([0-9]{4})(ext|x)?([0-9]+)?$/i,
    //starts with 3 numbers, another 3 numbers, then 4 numbers. we either have ext or x, and then some more numbers (though we don't know how many)
    replace: '$1-$2-$3 $4 $5' //If $4 and $5 aren't there we remove the extra space with the .trim() call at the end.
  }, {
    name: 'phone12',
    pattern: /(\+[0-9]{2})([0-9]{2})([0-9]{4})([0-9]{4})/,
    //a group of two with a plus, another group of two, and then 2 groups of 4.
    replace: '$1 $2 $3 $4'
  }]; //Array of number maps

  var not_allowed = [
    /\./g, //no dots.
    /^\+?1-?/g, //removes leading 1's with option +1 or 1- or +1- syntax
    /[\(\)\-\s]/g //gets rid of both parenthesis characters, hyphens, and spaces.
  ]; //Array of not allowed characters
  var remove_not_allowed = function(str, n) {
    //str we are using,
    //patterns or strings we are not allowing. We simply erase them.
    $.each(n, function(i, ni) {
      str = str.replace(ni, '');
    }); //Loops through each object in n, replacing it.
    return str;
  };

  var mapped_nums = $.map(nums_to_try, function(phone) {
    var phone = remove_not_allowed(phone, not_allowed);
    //gets rid of all our non-allowed characters or matches
    $.each(numbers, function(i, rep) {
      //loops through each phone number array
      if (phone.match(rep.pattern)) { //Check that the pattern applies, otherwise nothing is going to work.
        phone = phone.replace(rep.pattern, rep.replace);
        //if it matches, replace.
        return; //if it matches, we don't want duplicate matches so we end.
      }

    });
    return phone.trim(); //returns our trimmed out phone number
  }); //end $.map call
  console.log(mapped_nums); //console check just to make sure we aren't adding any spaces to the end of our strings.
  $('#results').html(mapped_nums.join('\n')); //Quick way to test our results.
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<pre id="results">
2159018060    -> 215-901-8060
(215) 901 8060-> 215-901-8060
(215)-901-8060-> 215-901-8060
1.215.901.8060 -> 215-901-8060
+1-215-9018060 -> 215-901-8060
+1-215-901-8060 x233 -> 215-901-8060 x233
+1-215-901-8060 Ext 233 -> 215-901-8060 Ext 233
+44 20 7323 8299 -> +44 20 7323 8299
</pre>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.