In JavaScript regular expressions are object. Regular expressions are patterns used to match character combinations in strings.
Example 1
i for case-insensitive search
Example 2
Example 3
^ indicates the beginning of the string. Using a ^ meta character requires that the match start at the beginning.
\d indicates a digit character and the {5} following it means that there must be 5 consecutive digit characters.
$ indicates the end of the string. Using a $ meta character requires that the match end at the end of the string.
Example 4
Example 5
Example 6
Example 7
Matches end of input.
For example, /t$/ does not match the 't' in "eater", but does match it in "eat"
Example 8
Example 9
Example 10
The \w meta character is used to find a word character.
A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.
Example 11
validation password
password can contain digits
password can contain character lower or upper case
password must have length between 5 to 10
password can contain _ (underscore)
Example 12
validation password
password can contain digits
password can contain character lower or upper case
password must have length between 5 to 15
password can contain !@#$%^&*_ (special character)
Example 13
validation password
password should contain atleast one digit
password should contain atleast one special character
password can contain character lower or upper case
password must have length between 5 to 15
Example 14
validation password
password should contain atleast one digit
password should contain atleast one special character
password should contain atleast one upper case latter
password can contain character lower or upper case
password must have length between 5 to 15
Example 15
validation
atleast one number
atleast one lower case latter
atleast one upper case latter
must have more than 5 length
Example 1
i for case-insensitive search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "DEEPAK SISODIYA"; | |
var pattern = /deepak/i; | |
pattern.test(str); // true |
Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/abc/.test('abcde') // true | |
/abc/.test('abdre') // false |
Example 3
^ indicates the beginning of the string. Using a ^ meta character requires that the match start at the beginning.
\d indicates a digit character and the {5} following it means that there must be 5 consecutive digit characters.
$ indicates the end of the string. Using a $ meta character requires that the match end at the end of the string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// validation for only 5 digits | |
/^\d{5}$/.test('12345') // true | |
/^\d{5}$/.test('123456') // false | |
/^\d{5}$/.test('1234F') // false |
Example 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// digits only | |
/\d/.test('11111') // true | |
/\d/.test('FFFF') // false |
Example 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// digit not allowed | |
/[^0-9]/.test('111') // false | |
/[^0-9]/.test('asdasd') // true |
Example 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// characters only | |
/^[a-zA-Z]+$/.test('abcdes') // true | |
/^[a-zA-Z]+$/.test('abcdes123') // false |
Example 7
Matches end of input.
For example, /t$/ does not match the 't' in "eater", but does match it in "eat"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/t$/.test('eater') // false | |
/t$/.test('eat') // true | |
// both for small and capital latter | |
/[tT]$/.test('eaT') // true |
Example 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Matches begining of input. | |
/^t/.test('ate') // false | |
/^t/.test('tear') // true | |
/^[tT]/.test('Tear') // true |
Example 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// character and number only | |
/^[0-9a-zA-Z]+$/.test('abcdes123') // true | |
/^[0-9a-zA-Z]+$/.test('abcdes123@') // false |
Example 10
The \w meta character is used to find a word character.
A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/\w/.test('abcdes123_') // true | |
/^[0-9a-zA-Z_]+$/.test('abcdes123_') // true |
Example 11
validation password
password can contain digits
password can contain character lower or upper case
password must have length between 5 to 10
password can contain _ (underscore)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^[a-zA-Z0-9_]{5,10}$/.test('aqsZZ1234_') // true | |
/^[a-zA-Z0-9_]{5,10}$/.test('aqsZZ') // true | |
/^[a-zA-Z0-9_]{5,10}$/.test('aqsZ') // false | |
/^[a-zA-Z0-9_]{5,10}$/.test('aqsZZ@') // false |
Example 12
validation password
password can contain digits
password can contain character lower or upper case
password must have length between 5 to 15
password can contain !@#$%^&*_ (special character)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^[a-zA-Z0-9!@#$%^&*_]{5,15}$/.test('aqsZZ_!@#$%^&*') // true |
Example 13
validation password
password should contain atleast one digit
password should contain atleast one special character
password can contain character lower or upper case
password must have length between 5 to 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^(?=.*[0-9])(?=.*[!@#$%^&*_])(?=.*[A-Z])[a-zA-Z0-9!@#$%^&*_]{5,15}$/.test('ase123@A') // true |
Example 14
validation password
password should contain atleast one digit
password should contain atleast one special character
password should contain atleast one upper case latter
password can contain character lower or upper case
password must have length between 5 to 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^(?=.*[0-9])(?=.*[!@#$%^&*_])(?=.*[A-Z])[a-zA-Z0-9!@#$%^&*_]{5,15}$/.test('ase123@A') // true |
Example 15
validation
atleast one number
atleast one lower case latter
atleast one upper case latter
must have more than 5 length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{5,}$/.test('1aAsdf') // true | |
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{5,}$/.test('1aA sdf') // true | |
// with no spaces (spaces not allowed) | |
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?!\S).{5,}$/.test('1aA sdf') // false |