# List of handy Regular Expression examples for real-world applications

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.


```js
function isEmailValid(email) {
 if(/^[^@ ]+@[^@ ]+\.[^@ \.]{2,}$/.test(email)) {
  console.log('email is valid');
 } else {
  console.log('email is invalid')
 }
}

isEmailValid('abc@11gmail.com'); // email is valid
isEmailValid('abc11gmail.com'); // email is invalid
```

-----

## 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 *


```js
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'); // password is valid
isPasswordValid('Abc123'); // password is invalid
```
-----

## Valid date format


```
\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


```js
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'); // date is valid
isDateValid('2019-2-18'); // date is invalid
```
-----

## Empty string validation


```
^\s*$
```

This regular expression checks to see if the string contains only spaces


```js
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'); // string is not empty and does not contain spaces
isEmpty(''); // string is empty or contains only spaces
```
-----

## 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)


```js
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'); // phone number is valid
isValidPhone('(123)456-8911'); // phone number is invalid
```
-----

## 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}$`


```js
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'); // valid visa credit card number
isValidVisaCard('2111111111111'); // invalid visa credit card number
isValidVisaCard('411111111111'); // invalid visa credit card number
```


If you have difficulty understanding any of the above regular expressions, check out my [this article](https://medium.com/javascript-in-plain-english/how-to-easily-understand-any-regular-expression-in-the-world-46ff6a5f551c?source=friends_link&sk=b1ace0462bda6a97973a54e1b103c0d7) 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.

[<img src="https://cdn.buymeacoffee.com/buttons/default-yellow.png" >](https://www.buymeacoffee.com/myogeshchavan97)

**Don’t forget to subscribe to get my weekly newsletter with amazing tips, tricks, and articles directly in your inbox [here](https://yogeshchavan.dev).**
