Regular Expressions Examples: A Comprehensive List
Regular expressions are a powerful tool used for pattern matching in text. They are used in a wide range of applications like text editors, programming languages, and data validation. This article will provide a comprehensive list of regular expression examples.
Quick Guide
\ | the escape character – used to find an instance of a metacharacter like a period, brackets, etc. |
. | match any character except newline |
x | match any instance of x |
^x | match any character except x |
[x] | match any instance of x in the bracketed range – [abxyz] will match any instance of a, b, x, y, or z |
| | an OR operator – [x|y] will match an instance of x or y |
() | used to group sequences of characters or matches |
{} | used to define numeric quantifiers |
{x} | match must occur exactly x times |
{x,} | match must occur at least x times |
{x,y} | match must occur at least x times, but no more than y times |
? | preceding match is optional or one only, same as {0,1} |
* | find 0 or more of preceding match, same as {0,} |
+ | find 1 or more of preceding match, same as {1,} |
^ | match the beginning of the line |
$ | match the end of a line |
[:alpha:] | Represents an alphabetic character. Use [:alpha:]+ to find one of them. |
[:digit:] | Represents a decimal digit. Use [:digit:]+ to find one of them. |
[:alnum:] | Represents an alphanumeric character ([:alpha:] and [:digit:]). |
[:space:] | Represents a space character (but not other whitespace characters). |
[:print:] | Represents a printable character. |
[:cntrl:] | Represents a nonprinting character. |
[:lower:] | Represents a lowercase character if Match case is selected in Options. |
[:upper:] | Represents an uppercase character if Match case is selected in Options. |
\d | matches a digit, same as [0-9] |
\D | matches a non-digit, same as [^0-9] |
\s | matches a whitespace character (space, tab, newline, etc.) |
\S | matches a non-whitespace character |
\w | matches a word character |
\W | matches a non-word character |
\b | matches a word-boundary (NOTE: within a class, matches a backspace) |
\B | matches a non-wordboundary |
Table of Contents
- Email Address
- Number
- URL
- Percentage
- Username (Alphanumeric with underscore)
- Password (At least 8 characters, one uppercase, one lowercase, one digit, and one special character)
- Phone Number (US)
- ZIP Code (US)
- Hexadecimal Color Code (3 or 6 digits)
- IP Address (IPv4)
- Date & Time
- Card Validation
- Card CVV
- Credit Card Expiration Date (MM/YYYY)
- Credit Card Expiry Date (MM/YY)
- Credit Card Number (Visa, Mastercard, American Express, Discover)
- Amex Card Number
- BCGlobal Card Number
- Carte Blanche Card Number
- Diners Club Card Number
- Discover Card Number
- Insta Payment Card Number
- JCB Card Number
- Korean Local Card Number
- Laser Card Number
- Maestro Card Number
- Master Card Number
- Solo Card Number
- Switch Card Number
- Union Pay Card Number
- Visa Card Number
- Visa Master Card Number
Regular Expression with Example
Email Address
RegEx:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$
1234const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$/;Number
RegEx:
^[0-9]*$
12345const numberRegex = /^[0-9]*$/;console.log(numberRegex.test('abc')); // falseconsole.log(numberRegex.test('012345')); // trueconsole.log(numberRegex.test('0123456789')); // trueNumber for 10 digit
RegEx:
^[0-9]{10}$
12345const number10Regex = /^[0-9]{10}$/;console.log(number10Regex.test('abc')); // falseconsole.log(number10Regex.test('012345')); // falseconsole.log(number10Regex.test('0123456789')); // trueAllow symbols in number
Allow symbols:
-,+,(,)
RegEx:
^(?=.*[0-9])[- +()0-9]{10,14}$
12345const symbolsNumberRegex = /^(?=.*[0-9])[- +()0-9]{10,14}$/;console.log(symbolsNumberRegex.test('(999) 999-9999')); // trueconsole.log(symbolsNumberRegex.test('(999) 999-99.99')); // falseconsole.log(symbolsNumberRegex.test('56789')); // falseURL
RegEx:
^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$
1234const urlRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;console.log(urlRegex.test('https://example.com')); // trueconsole.log(urlRegex.test('example.com')); // truePercentage
RegEx:
^((\d{0,2}(\.\d{1,4})?)|100)$
1234const percentageRegex = /^((\d{0,2}(\.\d{1,4})?)|100)$/;console.log(percentageRegex.test('67.22')); // trueconsole.log(percentageRegex.test('999')); // falseUsername (Alphanumeric with underscore)
RegEx:
^[a-zA-Z0-9_]+$
1234const usernameRegex = /^[a-zA-Z0-9_]+$/;console.log(usernameRegex.test('user_123')); // trueconsole.log(usernameRegex.test('user@123')); // falsePassword (At least 8 characters, one uppercase, one lowercase, one digit, and one special character)
RegEx:
^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{8,}$
1234const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;console.log(passwordRegex.test('Password123!')); // trueconsole.log(passwordRegex.test('password123')); // falsePhone Number (US)
RegEx:
^(\d{3}-\d{3}-\d{4}|\d{10})$
1234const phoneRegex = /^(\d{3}-\d{3}-\d{4}|\d{10})$/;console.log(phoneRegex.test('123-456-7890')); // trueconsole.log(phoneRegex.test('1234567890')); // trueZIP Code (US)
RegEx:
^\d{5}$|^\d{5}-\d{4}$
1234const zipCodeRegex = /^\d{5}$|^\d{5}-\d{4}$/;console.log(zipCodeRegex.test('12345')); // trueconsole.log(zipCodeRegex.test('12345-6789')); // trueHexadecimal Color Code (3 or 6 digits)
RegEx:
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
1234const colorCodeRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;console.log(colorCodeRegex.test('#FFA500')); // trueconsole.log(colorCodeRegex.test('#ABC')); // trueIP Address (IPv4)
RegEx:
^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
1234const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;console.log(ipv4Regex.test('192.168.0.1')); // trueconsole.log(ipv4Regex.test('256.0.0.0')); // falseDate (YYYY-MM-DD)
RegEx:
^(\d{4})-(\d{2})-(\d{2})$
1234const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;console.log(dateRegex.test('2022-04-25')); // trueconsole.log(dateRegex.test('22-04-25')); // false24 Hour Time (hh:mm)
RegEx:
^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
1234const timeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;console.log(timeRegex.test('23:59')); // trueconsole.log(timeRegex.test('24:59')); // falseCard CVV
RegEx:
^[0-9]{3,4}$
1234const cardCVVRegex = /^[0-9]{3,4}$/;console.log(cardCVVRegex.test('123')); // trueconsole.log(cardCVVRegex.test('1234')); // trueCredit Card Expiration Date (MM/YYYY)
RegEx:
^(0[1-9]|1[0-2])\/(20)\d{2}$
1234const expirationDateRegex = /^(0[1-9]|1[0-2])\/(20)\d{2}$/;console.log(expirationDateRegex.test('06/2022')); // trueconsole.log(expirationDateRegex.test('13/2022')); // falseCredit Card Expiry Date (MM/YY)
RegEx:
(0[1-9]|10|11|12)\/[0-9]{2}|\.
1234const expiryDateRegex = /(0[1-9]|10|11|12)\/[0-9]{2}|\./;console.log(expiryDateRegex.test('06/22')); // trueconsole.log(expiryDateRegex.test('13/23')); // falseCredit Card Number (Visa, Mastercard, American Express, Discover)
RegEx:
^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$
1234const creditCardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/;console.log(creditCardRegex.test('4111111111111111')); // trueconsole.log(creditCardRegex.test('6011123456789011')); // trueAmex Card Number
RegEx:
^3[47][0-9]{13}$
1234const amexCardRegex = /^3[47][0-9]{13}$/;console.log(amexCardRegex.test('377400111111115')); // trueconsole.log(amexCardRegex.test('376000000000006')); // trueBCGlobal Card Number
RegEx:
^(6541|6556)[0-9]{12}$
1234const bcglobalCardRegex = /^(6541|6556)[0-9]{12}$/;console.log(bcglobalCardRegex.test('6556123456789012')); // trueconsole.log(bcglobalCardRegex.test('6556123456789006')); // trueCarte Blanche Card Number
RegEx:
^389[0-9]{11}$
1234const carteBlancheCardRegex = /^389[0-9]{11}$/;console.log(carteBlancheCardRegex.test('38912345678909')); // trueconsole.log(carteBlancheCardRegex.test('30569309025904')); // falseDiners Club Card Number
RegEx:
^3(?:0[0-5]|[68][0-9])[0-9]{11}$
1234const dinersClubCardRegex = /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/;console.log(dinersClubCardRegex.test('30569309025904')); // trueconsole.log(dinersClubCardRegex.test('38520000023237')); // trueDiscover Card Number
RegEx:
^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$
1234const discoverCardRegex = /^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$/;console.log(discoverCardRegex.test('6011111111111117')); // trueconsole.log(discoverCardRegex.test('6011000990139424')); // trueInsta Payment Card Number
RegEx:
^63[7-9][0-9]{13}$
1234const instaPaymentCardRegex = /^63[7-9][0-9]{13}$/;console.log(instaPaymentCardRegex.test('6377820200251698')); // trueconsole.log(instaPaymentCardRegex.test('6377820200251011')); // trueJCB Card Number
RegEx:
^(?:2131|1800|35\d{3})\d{11}$
1234const jcbCardRegex = /^(?:2131|1800|35\d{3})\d{11}$/;console.log(jcbCardRegex.test('3566000020000410')); // trueconsole.log(jcbCardRegex.test('3530111333300000')); // trueKorean Local Card Number
RegEx:
^9[0-9]{15}$
1234const koreanLocalCardRegex = /^9[0-9]{15}$/;console.log(koreanLocalCardRegex.test('9111111111111111')); // trueconsole.log(koreanLocalCardRegex.test('9011123456789011')); // trueLaser Card Number
RegEx:
^(6304|6706|6709|6771)[0-9]{12,15}$
1234const laserCardRegex = /^(6304|6706|6709|6771)[0-9]{12,15}$/;console.log(laserCardRegex.test('6304111111111111')); // trueconsole.log(laserCardRegex.test('6709123456789011')); // trueMaestro Card Number
RegEx:
^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$
1234const maestroCardRegex = /^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$/;console.log(maestroCardRegex.test('5018111111111111')); // trueconsole.log(maestroCardRegex.test('6759123456789011')); // trueMaster Card Number
RegEx:
^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$
1234const masterCardRegex = /^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/;console.log(masterCardRegex.test('5425233430109903')); // trueconsole.log(masterCardRegex.test('2223000048410010')); // trueSolo Card Number
RegEx:
^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$
1234const soloCardRegex = /^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$/;console.log(soloCardRegex.test('6334101999990016')); // trueconsole.log(soloCardRegex.test('6767123456789011')); // trueSwitch Card Number
RegEx:
^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$
1234const switchCardRegex = /^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$/;console.log(switchCardRegex.test('6331101999990016')); // trueconsole.log(switchCardRegex.test('6011123456789011')); // falseUnion Pay Card Number
RegEx:
^(62[0-9]{14,17})$
1234const unionPayCardRegex = /^(62[0-9]{14,17})$/;console.log(unionPayCardRegex.test('6292270001349812')); // trueconsole.log(unionPayCardRegex.test('629227000134981211')); // trueVisa Card Number
RegEx:
^4[0-9]{12}(?:[0-9]{3})?$
1234const visaCardRegex = /^4[0-9]{12}(?:[0-9]{3})?$/;console.log(visaCardRegex.test('4111111111111111')); // trueconsole.log(visaCardRegex.test('6011123456789011')); // falseVisa Master Card Number
RegEx:
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$
1234const visaMasterCardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$/;console.log(visaMasterCardRegex.test('4111111111111111')); // trueconsole.log(visaMasterCardRegex.test('6011123456789011')); // false