Regular expression is the most important part in form validations and it is widely used for search, replace and web crawling systems. If you want to write a selector engine (used to find elements in a DOM), it should be possible with Regular Expressions. In this post we explained few tips that how to understand and write the Regular Expression in simple way.

Author

Arun Kumar Sekar
Engineer, Plugin Expert
Chennai, INDIA
Engineer, Plugin Expert
Chennai, INDIA
Will discuss about basic regular expression in three stages.
Stage 1
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”
var A = /^abc$/;
Input start with “abc”
var B = /^abc/;
Input end with “abc”
var C = /abc$/;
Input “abc” and one character allowed Eg. abcx
var D = /^abc.$/;
Input “abc” and more than one character allowed Eg. abcxy
var E = /^abc.+$/;
Input exactly match with “abc.def”, cause (.) escaped
var F = /^abc\.def$/;
Passes any characters followed or not by “abc” Eg. abcxyz12....
var G = /^abc.+?$/
Stage 2
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.
var URL = /^(http|https|ftp):\/\/(www+\.)?[a-zA-Z0-9]+\.([a-zA-Z]{2,4})\/?/;
URL.test(“https://9lessons.info”); // pass
URL.test(“https://www.9lessons.info”); // pass
URL.test(“https://9lessons.info/”); // pass
URL.test(“https://9lessons.info/index.html”); // pass
URL.test(“https://9lessons.info”); // pass
URL.test(“https://www.9lessons.info”); // pass
URL.test(“https://9lessons.info/”); // pass
URL.test(“https://9lessons.info/index.html”); // pass
Stage 3
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
var number = /^(\+\d{2,4})?\s?(\d{10})$/; // validating phone number
number.test(1111111111); //pass
number.test(+111111111111); //pass
number.test(+11 1111111111); //pass
number.test(11111111); //Fail
number.test(1111111111); //pass
number.test(+111111111111); //pass
number.test(+11 1111111111); //pass
number.test(11111111); //Fail
Very nice post , Cool and simple
ReplyDeletenice share thank you bro
This is a great tutorial :) Can you write a tutorial for .htaccess like this ? it will be very help full. Thank you ! :)
ReplyDeleteGreat Work, Thanks for Sharing =)
ReplyDeletethanks
ReplyDeleteAmazing, finally a good explanation, very thanks.
ReplyDeleteSorry, but this test:
ReplyDeleteURL.test("http://www.9lessons.inoofscriptalert(1)/scriptoo");
pass too. So something is wrong.
It's very useful. thanks
ReplyDeleteya nice tutorial
ReplyDeleteit nice and simple ..
ReplyDeletevery nice and useful
ReplyDeletethanks for useful information
ReplyDeleteexplained very well :) thx
ReplyDeleteNice post. It was very helpful !
ReplyDeleteVery Nice Post.Thanks Dude
ReplyDeleteDont forget to include EMAIL Validation as well in this list :)
ReplyDeleteAmazing very good explanation
ReplyDeleteIt's very helpful for all the programmer...Thanks
ReplyDeleteThanks for sharing.... :)
ReplyDeleteVery helpful thanks
ReplyDeleteReally useful.. Great Tutorial Again.. :)
ReplyDeleteVery good article, really helpful. Thanks a ton.
ReplyDeleteI was looking for a tutorial on regular expression which simple as well as sharp. My searching stop here.... :)
ReplyDeletegreat job,very usseful,
ReplyDeletethanks for sharing
Nice Stuff
ReplyDeleteReally good job, Thanks
ReplyDeleteGood work , it is very helpful for all
ReplyDeleteThis is quit simple for all
Good
ReplyDeletenice thanks for posting...
ReplyDeleteGreat useful info
ReplyDeletevery useful... and well explained..
ReplyDeleteit will be helpfull -next post on .htaccess
ReplyDeletevery useful.. nice explanation
ReplyDeletegreat work dude
ReplyDeleteGreat help bro thanks
ReplyDeleteHi Arun.
ReplyDeleteGood Job. However my Suggestions are below.Correct me if i am wrong
Possible Errors are: multiple wwwwww will be accepted.
It should Be www? not www+
And
([a-zA-Z]{2,4})\/?/ it should be ([a-zA-Z]{2,4})(\/|\?|$) to restrict the .com,in,.edun,etc then / or ? then any thing else can come
So that below url ll be restricted
URL.test("http://www.9lessons.inoofscriptalert(1)/scriptoo");
The Final Regex is:
"/^(http|https|ftp):\/\/(www?\.)?[a-zA-Z0-9]+\.([a-zA-Z]{2,4})(\/|\?|$)/"
great tutorial,,,tanks bro
ReplyDelete? => Zero or one character (instead)
ReplyDeleteAwesome bro. Helpful article for newbies.
ReplyDeleteGreat! Thanks for the post dude, very helpful
ReplyDeletegood and nice tutorial...very helpfull
ReplyDeleteGreat lesson! Thanks
ReplyDeleteVery nice! You help us more!!
ReplyDeleteI am looking for what is the use of Required.Js and how to use in our web apps.
ReplyDeletevery well explained.
ReplyDeleteNice
ReplyDeletefrom where u got such a deep knowledge
ReplyDeleteNice Article. Most of the time i didn't understand why $ \ so it's very helpfull for me...
ReplyDeleteSimple and Powerful
ReplyDeleteThanks
Please Update:
ReplyDelete/^(http|https|ftp):\/\/(www?\.)?[a-zA-Z0-9]+\.([a-zA-Z]{2,4})(\/|\?|$)/
Great............
ReplyDeleteSimple and useful....thanks fir sharing!
ReplyDeleteThanks for the post Great article the information published here will be very useful. anyway thanks again. nice work :D
ReplyDeleteClassic Tutorial yar...
ReplyDeleteVery Nice topics
ReplyDeleteVery Nice topics
ReplyDeletewooo
ReplyDeleteThank
ReplyDeleteVery Nice Post :)
ReplyDeleteIt's Help me a lot.
Helps me a lot
ReplyDeletecool
ReplyDeleteit's Very Cool Post Thanks Arun
ReplyDeleteNicely Explained Bro. Always had trouble understanding them. Thanks
ReplyDeleteCool! Thanks for the tutorial!
ReplyDeleteExcellent
ReplyDeleteHi,
ReplyDeleteyou don't know how much you made my coding life easier, thanks a million!!
Awesome tutorial.
ReplyDeleteGreat Tutorial
ReplyDeleteit-sharks.com
Thnks a lot
ReplyDeleteThanks for the great tutorial arun.keep going
ReplyDeleteThank you for this wonderful article published in this page. This is very nice. I really liked it.
ReplyDeletethank q for ur explanation
ReplyDeletethanks 4 u :)
ReplyDeleteGood explanation
ReplyDeleteVery useful for beginners
ReplyDeleteReally like this post. Thanks for sharing it bro
ReplyDeleteNice tutorial
ReplyDeleteThank you it is very useful.And explanation also Superb.
ReplyDeleteSimple, precise and straight to the point. Thank you for this.
ReplyDelete