In this article, we will see some of the useful regular expressions which you can use in your real-world applications
Email validation
^[^@ ]+@[^@ ]+\.[^@ \.]{2,}$
^[^@ ]+ => starts with the character which is not @ and space (one or more occurrence of character)
@[^@ ]+ => then one single @ symbol and after that no @ symbol and no space allowed(one or more occurrence of character)
\.[^@ \.]{2,}$ => then one dot and then at least 2 characters which does not include @, space and dot.
function isEmailValid(email) {
if(/^[^@ ]+@[^@ ]+\.[^@ \.]{2,}$/.test(email)) {
console.log('email is valid');
} else {
console.log('email is invalid')
}
}
isEmailValid('abc@11gmail.com');
isEmailValid('abc11gmail.com');
Password validation
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)(?=.*[!@#$*])
This regular expression checks for a Password which should contain at least one uppercase letter, lowercase letter, digit, and special symbol
(?=.*\d) => checks for something followed by zero or more occurrence of digit
(?=.*[a-z]) => checks for something followed by zero or more occurrence of lowercase letter
(?=.*[A-Z]) => checks for something followed by zero or more occurrence of uppercase letter
(?!.*\s) => checks for something which is non-space character
(?=.*[!@#$*]) => checks for something followed by zero or more occurrence of special characters from the list !, @, #, $ and *
function isPasswordValid(password) {
if(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)(?=.*[!@#$*])/.test(password)) {
console.log('password is valid');
} else {
console.log('password is invalid')
}
}
isPasswordValid('Abc@123');
isPasswordValid('Abc123');
\d{4}-\d{2}-\d{2}
This regular expression checks if a date is entered in YYYY–MM–YY format
Like 2019–12–18 i.e 4 digits–2 digits–2 digits
function isDateValid(date) {
if(/\d{4}-\d{2}-\d{2}/.test(date)) {
console.log('date is valid');
} else {
console.log('date is invalid')
}
}
isDateValid('2019-12-18');
isDateValid('2019-2-18');
Empty string validation
^\s*$
This regular expression checks to see if the string contains only spaces
function isEmpty(value) {
if(/^\s*$/.test(value)) {
console.log('string is empty or contains only spaces');
} else {
console.log('string is not empty and does not contain spaces')
}
}
isEmpty('abc');
isEmpty('');
Phone number validation
^[\\(]\d{3}[\\)]\s\d{3}-\d{4}$
This regular expression checks for a phone number in the format (123) 456–8911 i.e (3 digits)(space)(3 digits)(hyphen)(4 digits)
function isValidPhone(phone) {
if(/^[\\(]\d{3}[\\)]\s\d{3}-\d{4}$/.test(phone)) {
console.log('phone number is valid');
} else {
console.log('phone number is invalid')
}
}
isValidPhone('(123) 456-8911');
isValidPhone('(123)456-8911');
Credit card number Validation
Visa Credit Card: ^4[0–9]{12}(?:[0–9]{3})?$
American Express Credit Card: ^3[47][0–9]{13}$
Mastercard: ^(?:5[1–5][0–9]{2}|222[1–9]|22[3–9][0–9]|2[3–6][0–9]{2}|27[01][0–9]|2720)[0–9]{12}$
Discover Card: ^6(?:011|5[0–9]{2})[0–9]{12}$
function isValidVisaCard(cardNumber) {
if(/^4[0-9]{12}(?:[0-9]{3})?$/.test(cardNumber)) {
console.log('valid visa credit card number');
} else {
console.log('invalid visa credit card number')
}
}
isValidVisaCard('4111111111111');
isValidVisaCard('2111111111111');
isValidVisaCard('411111111111');
If you have difficulty understanding any of the above regular expressions, check out my this article regarding how to easily understand any regular expression in the world.
That’s it for today. I hope you learned something new today.
I hope you've been enjoying my articles and tutorials I've been writing. If you found them useful, consider buying me a coffee! I would really appreciate it.

Don’t forget to subscribe to get my weekly newsletter with amazing tips, tricks, and articles directly in your inbox here.