Will discuss about basic regular expression in three stages.
Symbol Explanation
^ Start of string
$ End of string
. Any single character
+ One or more character
Escape Special characters
? Zero or more characters
Input exactly match with “abc”
Input start with “abc”
Input end with “abc”
Input “abc” and one character allowed Eg. abcx
Input “abc” and more than one character allowed Eg. abcxy
Input exactly match with “abc.def”, cause (.) escaped
Passes any characters followed or not by “abc” Eg. abcxyz12….
Char Group Explanation
[abc] Should match any single of character
[^abc] Should not match any single character
[a-zA-Z0-9] Characters range lowercase a-z, uppercase A-Z and numbers
[a-z-._] Match against character range lowercase a-z and ._- special chats
(.*?) Capture everything enclosed with brackets
(com|info) Input should be “com” or “info”
{2} Exactly two characters
{2,3} Minimum 2 characters and Maximum 3 characters
{2,} More than 2 characters
Put together all in one URL validation.
URL.test(“http://www.9lessons.info”); // pass
URL.test(“https://9lessons.info/”); // pass
URL.test(“http://9lessons.info/index.html”); // pass
Short Form Equivalent Explanation
d [0-9] Any numbers
D [^0-9] Any non-digits
w [a-zA-Z0-9_] Characters,numbers and underscore
W [^a-zA-Z0-9_] Except any characters, numbers and underscore
s – White space character
S – Non white space character
number.test(+111111111111); //pass
number.test(+11 1111111111); //pass
number.test(11111111); //Fail