A regular expression, or regex, is a sequence of characters that specifies a match pattern in text. Often times regex is used for form validations and string-searching algorithms. Here are a few regex that are handy for developers:
Letters
- Domain:
^([a-z][a-z0-9-]+(\.|-*\.))+[a-z]{2,6}$
- Email:
^[_]*([a-z0-9]+(\.|_*)?)+@([a-z][a-z0-9-]+(\.|-*\.))+[a-z]{2,6}$
- Letters only:
^[a-zA-Z]+$
Numbers
- Numbers only:
^[0-9]*$
- Positive numbers:
^\d*\.?\d+$
- Negative numbers:
^-\d*\.?\d+$
- Numbers with spaces and parenthesis:
/^(?:\+\d{1,3}|0\d{1,3}|00\d{1,2})?(?:\s?\(\d+\))?(?:[-\/\s.]|\d)+$/
- Phone number:
^\+?[\d\s]{3,}$
- Phone number with code:
^\+?[\d\s]+\(?[\d\s]{10,}$
Useful for Inputs
- Letter and numbers only:
^[A-Z0-9]+$
- Match blank input:
^\s\t*$
- Match no input:
^$
- Match new line:
[\r\n]|$
- Match URL:
^http\:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$
- Match white space:
^\s+$
- Match multiple white space:
^.\s{2,}.$