Tuesday, April 28, 2015

Regular expression in JavaScript

In JavaScript regular expressions are object. Regular expressions are patterns used to match character combinations in strings.

Example 1
i for case-insensitive search

var str = "DEEPAK SISODIYA";
var pattern = /deepak/i;
pattern.test(str); // true
view raw gistfile1.js hosted with ❤ by GitHub

Example 2

/abc/.test('abcde') // true
/abc/.test('abdre') // false
view raw gistfile1.js hosted with ❤ by GitHub

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.

// validation for only 5 digits
/^\d{5}$/.test('12345') // true
/^\d{5}$/.test('123456') // false
/^\d{5}$/.test('1234F') // false
view raw gistfile1.js hosted with ❤ by GitHub

Example 4

// digits only
/\d/.test('11111') // true
/\d/.test('FFFF') // false
view raw gistfile1.js hosted with ❤ by GitHub

Example 5

// digit not allowed
/[^0-9]/.test('111') // false
/[^0-9]/.test('asdasd') // true
view raw gistfile1.js hosted with ❤ by GitHub

Example 6

// characters only
/^[a-zA-Z]+$/.test('abcdes') // true
/^[a-zA-Z]+$/.test('abcdes123') // false
view raw gistfile1.js hosted with ❤ by GitHub

Example 7
Matches end of input.
For example, /t$/ does not match the 't' in "eater", but does match it in "eat"

/t$/.test('eater') // false
/t$/.test('eat') // true
// both for small and capital latter
/[tT]$/.test('eaT') // true
view raw gistfile1.js hosted with ❤ by GitHub

Example 8

// Matches begining of input.
/^t/.test('ate') // false
/^t/.test('tear') // true
/^[tT]/.test('Tear') // true
view raw gistfile1.js hosted with ❤ by GitHub

Example 9

// character and number only
/^[0-9a-zA-Z]+$/.test('abcdes123') // true
/^[0-9a-zA-Z]+$/.test('abcdes123@') // false
view raw gistfile1.js hosted with ❤ by GitHub

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.

/\w/.test('abcdes123_') // true
/^[0-9a-zA-Z_]+$/.test('abcdes123_') // true
view raw gistfile1.js hosted with ❤ by GitHub

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)

/^[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
view raw gistfile1.js hosted with ❤ by GitHub

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)

/^[a-zA-Z0-9!@#$%^&*_]{5,15}$/.test('aqsZZ_!@#$%^&*') // true
view raw gistfile1.js hosted with ❤ by GitHub

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

/^(?=.*[0-9])(?=.*[!@#$%^&*_])(?=.*[A-Z])[a-zA-Z0-9!@#$%^&*_]{5,15}$/.test('ase123@A') // true
view raw gistfile1.js hosted with ❤ by GitHub

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

/^(?=.*[0-9])(?=.*[!@#$%^&*_])(?=.*[A-Z])[a-zA-Z0-9!@#$%^&*_]{5,15}$/.test('ase123@A') // true
view raw gistfile1.js hosted with ❤ by GitHub

Example 15
validation
atleast one number
atleast one lower case latter
atleast one upper case latter
must have more than 5 length

/^(?=.*[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
view raw gistfile1.js hosted with ❤ by GitHub

No comments:

Post a Comment