cluemediator / regular-expression-examples

Regular Expressions Examples: A Comprehensive List

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regular Expressions Examples: A Comprehensive List

Thank you for checking out this project! If you find it useful, please consider giving it a star . Pull requests are always welcome and greatly appreciated. For more technical updates, follow us on Twitter @ClueMediator.

Note: This repository is dedicated to showcasing JavaScript Regular Expression examples only. If you're interested in more technical content, please visit Clue Mediator's GitHub profile.

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

Regular Expression with Example

  • Email Address

    RegEx: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$

    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$/;
    console.log(emailRegex.test('example@example.com')); // true
    console.log(emailRegex.test('example@.com')); // false

    Back to Top

  • Number

    RegEx: ^[0-9]*$

    const numberRegex = /^[0-9]*$/;
    console.log(numberRegex.test('abc')); // false
    console.log(numberRegex.test('012345')); // true
    console.log(numberRegex.test('0123456789')); // true

    ⬆ Back to Top

  • Number for 10 digit

    RegEx: ^[0-9]{10}$

    const number10Regex = /^[0-9]{10}$/;
    console.log(number10Regex.test('abc')); // false
    console.log(number10Regex.test('012345')); // false
    console.log(number10Regex.test('0123456789')); // true

    Back to Top

  • Allow symbols in number

    Allow symbols: -,+,(,)

    RegEx: ^(?=.*[0-9])[- +()0-9]{10,14}$

    const symbolsNumberRegex = /^(?=.*[0-9])[- +()0-9]{10,14}$/;
    console.log(symbolsNumberRegex.test('(999) 999-9999')); // true
    console.log(symbolsNumberRegex.test('(999) 999-99.99')); // false
    console.log(symbolsNumberRegex.test('56789')); // false

    ⬆ Back to Top

  • URL

    RegEx: ^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

    const urlRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
    console.log(urlRegex.test('https://example.com')); // true
    console.log(urlRegex.test('example.com')); // true

    Back to Top

  • Percentage

    RegEx: ^((\d{0,2}(\.\d{1,4})?)|100)$

    const percentageRegex = /^((\d{0,2}(\.\d{1,4})?)|100)$/;
    console.log(percentageRegex.test('67.22')); // true
    console.log(percentageRegex.test('999')); // false

    Back to Top

  • Username (Alphanumeric with underscore)

    RegEx: ^[a-zA-Z0-9_]+$

    const usernameRegex = /^[a-zA-Z0-9_]+$/;
    console.log(usernameRegex.test('user_123')); // true
    console.log(usernameRegex.test('user@123')); // false

    Back to Top

  • Password (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,}$

    const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
    console.log(passwordRegex.test('Password123!')); // true
    console.log(passwordRegex.test('password123')); // false

    Back to Top

  • Phone Number (US)

    RegEx: ^(\d{3}-\d{3}-\d{4}|\d{10})$

    const phoneRegex = /^(\d{3}-\d{3}-\d{4}|\d{10})$/;
    console.log(phoneRegex.test('123-456-7890')); // true
    console.log(phoneRegex.test('1234567890')); // true

    Back to Top

  • ZIP Code (US)

    RegEx: ^\d{5}$|^\d{5}-\d{4}$

    const zipCodeRegex = /^\d{5}$|^\d{5}-\d{4}$/;
    console.log(zipCodeRegex.test('12345')); // true
    console.log(zipCodeRegex.test('12345-6789')); // true

    Back to Top

  • Hexadecimal Color Code (3 or 6 digits)

    RegEx: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

    const colorCodeRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
    console.log(colorCodeRegex.test('#FFA500')); // true
    console.log(colorCodeRegex.test('#ABC')); // true

    ⬆ Back to Top

  • IP 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]?)$

    const 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')); // true
    console.log(ipv4Regex.test('256.0.0.0')); // false

    Back to Top

  • Date (YYYY-MM-DD)

    RegEx: ^(\d{4})-(\d{2})-(\d{2})$

    const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
    console.log(dateRegex.test('2022-04-25')); // true
    console.log(dateRegex.test('22-04-25')); // false

    Back to Top

  • 24 Hour Time (hh:mm)

    RegEx: ^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

    const timeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
    console.log(timeRegex.test('23:59')); // true
    console.log(timeRegex.test('24:59')); // false

    Back to Top

  • Card CVV

    RegEx: ^[0-9]{3,4}$

    const cardCVVRegex = /^[0-9]{3,4}$/;
    console.log(cardCVVRegex.test('123')); // true
    console.log(cardCVVRegex.test('1234')); // true

    Back to Top

  • Credit Card Expiration Date (MM/YYYY)

    RegEx: ^(0[1-9]|1[0-2])\/(20)\d{2}$

    const expirationDateRegex = /^(0[1-9]|1[0-2])\/(20)\d{2}$/;
    console.log(expirationDateRegex.test('06/2022')); // true
    console.log(expirationDateRegex.test('13/2022')); // false

    Back to Top

  • Credit Card Expiry Date (MM/YY)

    RegEx: (0[1-9]|10|11|12)\/[0-9]{2}|\.

    const expiryDateRegex = /(0[1-9]|10|11|12)\/[0-9]{2}|\./;
    console.log(expiryDateRegex.test('06/22')); // true
    console.log(expiryDateRegex.test('13/23')); // false

    Back to Top

  • Credit 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})$

    const 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')); // true
    console.log(creditCardRegex.test('6011123456789011')); // true

    Back to Top

  • Amex Card Number

    RegEx: ^3[47][0-9]{13}$

    const amexCardRegex = /^3[47][0-9]{13}$/;
    console.log(amexCardRegex.test('377400111111115')); // true
    console.log(amexCardRegex.test('376000000000006')); // true

    Back to Top

  • BCGlobal Card Number

    RegEx: ^(6541|6556)[0-9]{12}$

    const bcglobalCardRegex = /^(6541|6556)[0-9]{12}$/;
    console.log(bcglobalCardRegex.test('6556123456789012')); // true
    console.log(bcglobalCardRegex.test('6556123456789006')); // true

    Back to Top

  • Carte Blanche Card Number

    RegEx: ^389[0-9]{11}$

    const carteBlancheCardRegex = /^389[0-9]{11}$/;
    console.log(carteBlancheCardRegex.test('38912345678909')); // true
    console.log(carteBlancheCardRegex.test('30569309025904')); // false

    Back to Top

  • Diners Club Card Number

    RegEx: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$

    const dinersClubCardRegex = /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/;
    console.log(dinersClubCardRegex.test('30569309025904')); // true
    console.log(dinersClubCardRegex.test('38520000023237')); // true

    Back to Top

  • Discover 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})$

    const 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')); // true
    console.log(discoverCardRegex.test('6011000990139424')); // true

    Back to Top

  • Insta Payment Card Number

    RegEx: ^63[7-9][0-9]{13}$

    const instaPaymentCardRegex = /^63[7-9][0-9]{13}$/;
    console.log(instaPaymentCardRegex.test('6377820200251698')); // true
    console.log(instaPaymentCardRegex.test('6377820200251011')); // true

    ⬆ Back to Top

  • JCB Card Number

    RegEx: ^(?:2131|1800|35\d{3})\d{11}$

    const jcbCardRegex = /^(?:2131|1800|35\d{3})\d{11}$/;
    console.log(jcbCardRegex.test('3566000020000410')); // true
    console.log(jcbCardRegex.test('3530111333300000')); // true

    ⬆ Back to Top

  • Korean Local Card Number

    RegEx: ^9[0-9]{15}$

    const koreanLocalCardRegex = /^9[0-9]{15}$/;
    console.log(koreanLocalCardRegex.test('9111111111111111')); // true
    console.log(koreanLocalCardRegex.test('9011123456789011')); // true

    Back to Top

  • Laser Card Number

    RegEx: ^(6304|6706|6709|6771)[0-9]{12,15}$

    const laserCardRegex = /^(6304|6706|6709|6771)[0-9]{12,15}$/;
    console.log(laserCardRegex.test('6304111111111111')); // true
    console.log(laserCardRegex.test('6709123456789011')); // true

    ⬆ Back to Top

  • Maestro Card Number

    RegEx: ^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$

    const maestroCardRegex = /^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$/;
    console.log(maestroCardRegex.test('5018111111111111')); // true
    console.log(maestroCardRegex.test('6759123456789011')); // true

    Back to Top

  • Master 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}))$

    const 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')); // true
    console.log(masterCardRegex.test('2223000048410010')); // true

    Back to Top

  • Solo Card Number

    RegEx: ^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$

    const soloCardRegex = /^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$/;
    console.log(soloCardRegex.test('6334101999990016')); // true
    console.log(soloCardRegex.test('6767123456789011')); // true

    ⬆ Back to Top

  • Switch 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}$

    const 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')); // true
    console.log(switchCardRegex.test('6011123456789011')); // false

    Back to Top

  • Union Pay Card Number

    RegEx: ^(62[0-9]{14,17})$

    const unionPayCardRegex = /^(62[0-9]{14,17})$/;
    console.log(unionPayCardRegex.test('6292270001349812')); // true
    console.log(unionPayCardRegex.test('629227000134981211')); // true

    Back to Top

  • Visa Card Number

    RegEx: ^4[0-9]{12}(?:[0-9]{3})?$

    const visaCardRegex = /^4[0-9]{12}(?:[0-9]{3})?$/;
    console.log(visaCardRegex.test('4111111111111111')); // true
    console.log(visaCardRegex.test('6011123456789011')); // false

    Back to Top

  • Visa Master Card Number

    RegEx: ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$

    const visaMasterCardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$/;
    console.log(visaMasterCardRegex.test('4111111111111111')); // true
    console.log(visaMasterCardRegex.test('6011123456789011')); // false

    Back to Top


Happy Coding...!! 😊

About

Regular Expressions Examples: A Comprehensive List