Regular expression examples in JavaScript
Today we will provide you a list of regular expression examples in JavaScript. While you are working with the real project then you might need to implement certain validation so these examples are helpful to you over there.
Regular expression examples in JavaScript, regex syntax, regular expression pattern matching example, regular expression for email, numbers only, mobile number, form validation, name and password, URL, CVV, Expiry Date.
Checkout more articles on JavaScript
- Switch Case Statement in JavaScript
- Remove Duplicate Values from an Array in JavaScript
- Detect Browsers in JavaScript
- Map, Filter and Reduce Operators in JavaScript
- Spread Operator in JavaScript
Syntax: Validate the value by regular expression
var value = "xxxxxx";
var regex = /^xxxxxxxxx$/;
return regex.test(value);
Let’s take an example for different types of validations using regular expressions. It’s also possible to use different regex for the same validation.
Regular expression with examples
- Email validation
- Number validation
- Number validation for 10 digit
- Allow symbols in number validation
- URL validation
- Card CVV validation
- Card expiry date validation
- Percentage validation
- Password validation
1. Email validation
Regular expression for email validation.
var regEx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
regEx.test("abc"); // Output: false
regEx.test("abc@"); // Output: false
regEx.test("abc@sdaf"); // Output: false
regEx.test("[email protected]"); // Output: false
regEx.test("[email protected]"); // Output: true
regEx.test("[email protected]"); // Output: true
2. Number validation
Use below regular expression to validate the number value.
var regEx = /^[0-9]*$/;
regEx.test("abc"); // Output: false
regEx.test(); // Output: false
regEx.test("0123"); // Output: true
regEx.test("0123456789"); // Output: true
3. Number validation for 10 digit
Regular expression to validate the 10 digit number.
var regEx = /^[0-9]{10}$/;
regEx.test("abc"); // Output: false
regEx.test(); // Output: false
regEx.test("0123456789"); // Output: true
4. Allow symbols in number validation
Regular expression to allow the few symbols `like -,+,(,) including space` in number validation between 10 to 14 digits.
var regEx = /^(?=.*[0-9])[- +()0-9]{10,14}$/;
regEx.test("56789"); // Output: false
regEx.test("(999) 999-99.99"); // Output: false
regEx.test("(999) 999-9999"); // Output: true
5. URL validation
In order to validate the URL, use the regular expression below.
var regEx = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
regEx.test("cluemediator"); // Output: false
regEx.test("cluemediator.c"); // Output: false
regEx.test("cluemediator.com"); // Output: true
regEx.test("www.cluemediator.com"); // Output: true
regEx.test("test://www.cluemediator.com"); // Output: false
regEx.test("http://www.cluemediator.com"); // Output: true
regEx.test("https://www.cluemediator.com"); // Output: true
6. Card CVV validation
Use below regular expression to validate the card CVV number.
var regEx = /^[0-9]{3,4}$/;
regEx.test(""); // Output: false
regEx.test("123"); // Output: true
regEx.test("1234"); // Output: true
regEx.test("123456"); // Output: false
7. Card expiry date validation
To validate the card expiry date, use the regular expression below. Here we will consider the value in `mm/YY` format.
var regEx = /(0[1-9]|10|11|12)\/[0-9]{2}|\./;
regEx.test(""); // Output: false
regEx.test("8/22"); // Output: false
regEx.test("08/22"); // Output: true
regEx.test("12/22"); // Output: true
regEx.test("13/22"); // Output: false
regEx.test("08-22"); // Output: false
8. Percentage validation
Use below regular expression to validate the percentage value.
var regEx = /^((\d{0,2}(\.\d{1,4})?)|100)$/;
regEx.test("10"); // Output: true
regEx.test("67.22"); // Output: true
regEx.test("999"); // Output: false
regEx.test("100"); // Output: true
Here we allowed the 4 digits after the decimal point.
9. Password validation
Check below regular expression to validate the password in certain criteria. Like password must contain at least 8 characters, including uppercase, lowercase and numbers.
var regEx = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
regEx.test("123456789"); // Output: false
regEx.test("123456789d"); // Output: false
regEx.test("123456789dD"); // Output: true
regEx.test("ABC@123456"); // Output: false
regEx.test("ABCabc@123456"); // Output: true
That’s it for today.
Thank you for reading. Happy Coding!