9lessons programming blog
Loading Search
9lessons Bring Friends
Wall Script
Follow Me:
Monday, October 21, 2013

Understanding Regular Expression

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.

10 Reasons To Get A .BLOG Name Now


Author
Arun Kumar Sekar
Arun Kumar Sekar

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(“http://9lessons.info”);                      // pass
URL.test(“http://www.9lessons.info”);            // pass
URL.test(“https://9lessons.info/”);                   // pass
URL.test(“http://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
Comments
{ 61 comments }
Salih Hanifeoglu said...

Very nice post , Cool and simple
nice share thank you bro

Maxuio said...

This is a great tutorial :) Can you write a tutorial for .htaccess like this ? it will be very help full. Thank you ! :)

Matt Hernandez said...

Great Work, Thanks for Sharing =)

Anonymous said...

thanks

Anonymous said...

Amazing, finally a good explanation, very thanks.

Anonymous said...

Sorry, but this test:
URL.test("http://www.9lessons.inoofscriptalert(1)/scriptoo");
pass too. So something is wrong.

denaramdani said...

It's very useful. thanks

Anonymous said...

ya nice tutorial

varatha raj said...

it nice and simple ..

Amar Chowdary said...

very nice and useful

shalini shrotriya said...

thanks for useful information

chandan said...

explained very well :) thx

Datshay Sherpa said...

Nice post. It was very helpful !

Manish Patel said...

Very Nice Post.Thanks Dude

Jay said...

Dont forget to include EMAIL Validation as well in this list :)

shabbir said...

Amazing very good explanation

arinjit das said...

It's very helpful for all the programmer...Thanks

Aman Tilak said...

Thanks for sharing.... :)

Anonymous said...

Very helpful thanks

Karthik said...

Really useful.. Great Tutorial Again.. :)

Arkaprava Majumder said...

Very good article, really helpful. Thanks a ton.

Sajib.... said...

I was looking for a tutorial on regular expression which simple as well as sharp. My searching stop here.... :)

vikrant said...

great job,very usseful,


thanks for sharing

Anonymous said...

Nice Stuff

Dinidu Hewage said...

Really good job, Thanks

malla reddy Thukkani said...

Good work , it is very helpful for all
This is quit simple for all

hgh said...

Good

Ram Kumar said...

nice thanks for posting...

Srikanth said...

Great useful info

Anonymous said...

very useful... and well explained..

Sany Ahmed said...

it will be helpfull -next post on .htaccess

Amith Kumar said...

very useful.. nice explanation

javed sheikh said...

great work dude

aRjUn rEdDy said...

Great help bro thanks

Poovannan said...

Hi Arun.

Good 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})(\/|\?|$)/"

harry suherman said...

great tutorial,,,tanks bro

Anonymous said...

? => Zero or one character (instead)

daksh said...

Awesome bro. Helpful article for newbies.

Mohamed UL Haleeth said...

Great! Thanks for the post dude, very helpful

Anonymous said...

good and nice tutorial...very helpfull

Anonymous said...

Great lesson! Thanks

Virtual Geek said...

Very nice! You help us more!!

yogaraj s said...

I am looking for what is the use of Required.Js and how to use in our web apps.

Anonymous said...

very well explained.

Chandrababu Naidu said...

Nice

amit mehta said...

from where u got such a deep knowledge

Anonymous said...

Nice Article. Most of the time i didn't understand why $ \ so it's very helpfull for me...

Er.Praveen* said...

Simple and Powerful
Thanks

shashank saxena said...

Please Update:

/^(http|https|ftp):\/\/(www?\.)?[a-zA-Z0-9]+\.([a-zA-Z]{2,4})(\/|\?|$)/

Anonymous said...

Great............

Nitesh said...

Simple and useful....thanks fir sharing!

harsh said...

Thanks for the post Great article the information published here will be very useful. anyway thanks again. nice work :D

Anonymous said...

Classic Tutorial yar...

Ajay Kumar said...

Very Nice topics

Ajay Kumar said...

Very Nice topics

rahmatul.hidayat said...

wooo

Sun Vichea said...

Thank

Harshil Barot said...

Very Nice Post :)
It's Help me a lot.

Rahul Verma said...

Helps me a lot

thavisai halaphone said...

cool

Arun Raj said...

it's Very Cool Post Thanks Arun

Post a Comment