Commonly Used Regexes
Click on the pattern name to show or hide it. Copy the pattern to the sandbox to test it.
IP Address Pattern
\b(((\d{1,2})|([0-1]\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|([0-1]\d{2})|(2[0-4]\d)|(25[0-5]))\b\d{1,2}permits numbers between 0 and 99[0-1]\d{2}permits numbers between 100 and 199 and numbers bearing the form 0nn2[0-4]\dpermits numbers between 200 and 24925[0-5]permits numbers from 250 to 255- We combine the four patterns above into an alternation and append a
\.to enforce a . after each 2 or 3 digit number. {3}requires three sets of such numbers - nn. or nnn. to be provided.- We repeat the 0-99, 100-199/0nn, 200-249 and 250-255 alternation pattern one more time to test for the final two/three digit number - without the terminating .
- Wrap the pattern in word boundary constraints
\bto ensure that the first & fourth block of two/three digit numbers are not preceeded/followed by an illegal character.
Credit Card Pattern
((4\d{3})|(5[1-5]\d{2})|(6011))( |)?\d{4}(\5\d{4}){2}4d{3}- permits Visa card numbers, which always start with the digit 4.5[1-5]d{2}- permits Mastercard numbers which start with 51-55.6011- permits Discover numbers which always start with 6011.- We combine these three patterns into an alternation
( |)?\d{4}- requires a set of four digits optionally preceeded by a space.(\5\d{4}){2}- requires two more sets of four digits preceeded by a space if one was used prior to the second set, as described below.\5is a back reference - back references in regexes are simply numbered from left to right, irrespective of any nesting of the individual patterns.
Note that this pattern validator does not ensure that the number provided matches a real credit card. Credit card numbers contain an issuer identifier and a final check digit which must be valid. An exhaustive discussion of such matters is available here.
US Telephone Number Pattern
\(?[2-9]\d\d\)?(-| |\.)[2-9]\d\d\1\d{4}We impose the following rules
- The number is presented as three blocks of digits separated by a space a hyphen or a dot.
- Separator characters cannot mixed. i.e. (574) 975 1571 is legal but (574) 975.1571 is not.
- The number begins with a three digit prefix. The first of these digits cannot be 0 or 1.
- The prefix may be placed in parentheses.
- The second block of digits consists of three numbers with the first digit never being 0 or 1.
- The final block is made up of any four digits.
For the most part it is obvious how these rules are enforced by the pattern above. However, there are two features that merit a comment
We instruct the regex engine to index the first separator character for backreferencing by using the code ([ \.-]). At the location of the second separator character we use the backreference code \1. This ensures that numbers with mixed separator characters are not accepted.
This pattern will quite happily accept numbers bearing the form (574.975.1571 or 574) 975 1571. This could be easily remedied using conditional matching - a feature not supported by JavaScript.
Social Security Number Pattern
\d{3}-\d{2}-\d{4}This is an easy pattern to match. However, it will not weed out invalid numbers - e.g. all zeros or numbers where the first three digits are greater than 728. A pure, single-step, regex solution for handling such matters would be too complex. A better approach is to follow up the regex test with a closer examination of user input using a combination of additonal regexes and/or JavaScript string manipulation.
Do not wrap the model expression in a /.../ pair. The characters
^$.?*!+:=()[]{}|\\ must be escaped - except when then occur inside a character class. Invalid characters will be grayed out.| Result | Left Text | Match | Right Text |
|---|---|---|---|
Download