Removing that dash will keep it from following the pattern.Ĭase 4: Every dash, space, or period you add between digits in front of the last 8 digits (including a dash) will cause the expression to return the first digit and everything between it and the final eight digits up to 19 total digits, including the dashes, etc. You will see that pattern grayed out in all of my examples below.Ĭase 1: If you are trying to pull a number that shows right after a dash, space, or plus sign, it’ll include those as part of the pattern found.Ĭase 2: The version with the plus sign is inappropriate for international uses.Ĭase 3: If the last four numbers of a sequence of 18 digits have a dash, then it’ll return that number with everything before it. In all cases, a dash, period, or space must precede the last four digits and that separator must have at least 3 digits in front of it (e.g. It’s unlikely you will meet the following case scenarios, but I want to point them out just in case. Versions:Ġ00-0000 Issues You Can Expect with This Expression You can replace zeros with any digit 0-9. The following are examples of the phone number patterns that the expression above will find. You can also have a plus sign at the front. Using the expression above, you can change out dashes with spaces or periods. Restricting the format to only numbers reduces errors by pulling incorrect numbers. This version will not work with letters in phone numbers (e.g. I list examples at the bottom of this article. At that point you can use the variable an any function that responds to pattern matching. You can also put the expression between the parenthesis in something like var patterns = new RegExp() as I used above, with or without quotes. Just don’t use quotes around the expression. In Javascript or PHP (and likely other programming languages), you can define a variable with the expression. Then you can use number_found and increment the number between the brackets to return each phone number found. To find several phone numbers, add a g to the end of the expression (e.g. Number_found = patterns.exec(inner_code) //This use of exec() will find the first occurrence only. Inner_code = “This is a typical United States number format: 1 (555) 555-1212” Areas in gray represent the options for spaces, dashes, periods, or sometimes nothing between numbers (e.g. The red areas, represent the numbered portions. I finally had the epiphany today that created one expression that covered most international and United States phone number formats. This process would naturally affect traditional internet browsers for traditional computers but I went down this path mostly for tracking phone number touch events in Google Analytics on only mobile devices. E.I have been looking all over the web for a regular expression to use in JavaScript functions to help with formatting phone number links and printing phone numbers on pages for iPhones, Android, and other mobile devices.Please leave a comment below and let us know! It would be great to see what others are using as well. The best solution for you is likely to check the source of the number to ensure its validity, although these regular expressions can still be useful if no such information is available.ĭid you find any use for these regular expressions? Do you have any other regex to share with the rest of the community? What’s your best regex to validate phone numbers? Just remember that international phone numbers can vary widely, and without context it can be difficult to determine which country codes are used or how to arrange these digits into a specific format. If you find that you need to parse, validate, or format phone numbers, the regular expressions found in this post will be a great help. Table Of Contents:Ī regular expression to format and validate US (North American) Phone Numbers: That’s why I created this handy collection of regexes that are great for phone numbers. If you aren’t careful when building regex patterns, you can waste a lot of time tracking down hard-to-find bugs. Regular expressions are powerful but can be easy to misuse or even get wrong. For many of us, knowing how to work with regular expressions is a huge time saver.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |