Regular Expression in Java Script:-
By Tanaya Karmakar
(Future Group of Engineering College,Kolkata)
Regular Expression :-
Regular Expression are basically throws a restriction on the input entered by the user in each field of a validation form.
Syntax of Regular Expression :-
var regexp=/pattern/
Why to use regular expression ?
Just think about my javascript validation form, threre was a field that wanted the user to enter his or her date of birth and for this “dd” ,”mm” and “yyyy” were provided.Now ,just assume that you are filling such kind of form to have your own ID and if you enter string (eg your name) in the above mentioned field or enter numbers in the “name” field or incompatibility between password and confirm password then there will be an incompatibility between the required and your entered input . To prevent such kind of incompatibility ,regular expressions are necessary.
What is pattern matching characters ?
Before moving on to the pattern matching characters we have to know what is pattern string. Pattern matching strings are basically search string that searches in the main string whether that string defined as pattern lies in the main string or not.
For example,
var pat=/php/ ;
This will search in the main string whether “php” is present in main string or not.
They are basically literal characters and metacharacters (eg ‘$’ , ‘^’ etc) that are required to define a pattern string that represent the search criteria or better to say search for a particular in the entered input.
Categories of Pattern Matching Characters :-
Pattern matching characters can be grouped in variuous categories. Once you about these categories , you can easily write your own regular expression and incorporate them where required.
The categories are:-
Position Matching :- This is required to match a substring that occurs at a specific position in the main string. For example , a substring that occurs at the beginning or at the end or at any position of the main string.
Special literal character matching :-All alphabetic and numeric characters by default match themselves literally in regular expressions. For example , to match a newline ,the syntax “\n” is used,while “\r” matches a carriage return.
Character classes matching :-In this category, individual characters are combined into character classes in order to form more complex matches by placing them in containers like square bracket.For example ,/[abc]/ matches either “a” or “b” or “c” in a string while /[a-zA-Z0-9]/ matches all alphaneumeric characters.
Repitition matching :-This matching is required whenever it is required to search for a certain repitition in a string .For example, to match “555” , the easy way to use /5{3}/.
Alternation and grouping matching :-This category is required whenever a group of characters are treated as a single entity . You can add an “OR” logic to your pattern matching.
Back reference matching :- This category is used whenever a particular match in the string is based on the result of the earlier match.
Detailed description of above mentioned categories:-
Position matching :-
Symbol Description Example
^ Matches the beginning of the string. /^The/ matches “the” in “The night”.
$ Matches the end of the string. /and$/ matches “and” in “Land”.
\b Matches any word boundary. /ly\b/ matches “ly” in “This is really cool”.
\B Matches any non-word boundary. /\Bor/ matches “or” in “normal”.
Literals:-
Symbol Description
\n Matches a newline character.
\f Matches a formal feed character.
\r Matches a carriage return character.
\t Matches a horizontal tab character.
\v Matches a vertical tab character.
\uxxxx Matches the ASCII character expressed by the UNICODE xxxx.
Character classes:-
Symbol Description
[xyz] Matches any one character enclosed in the character set. Hyphen can also be used to denote the range.
[^xyz] Matches any one character not enclosed in the character set.
\w Matches any alphaneumeric character including underscore.
\W Matches any single non word character (like [^a-zA-Z0-9]).
\d Matches any single digit (equivalent to [0-9]).
\D Matches any non digit (equivalent to [^0-9]).
\s Matches any single space character (equivalent to [\t\r\n\v\f]).
\S Matches any single non-space character (equivalent to [^\t\r\n\v\f]).
Repitition :-
Symbol Description
{x} Matches exactly x occurences of a regular expression.
{x,} Matches x or more occurences.
{x,y} Matches x to y number of occurences.
? Matches zero or one occurences. Equivalent to {0,1}.
* Match zero or more occurenences. Equivalent to {0,}.
Alternation & Grouping :-
Symbol Description
() Grouping characters together to create a clause. May be nested.
| Alternation combines clauses into one regular expression and then matches any of the individual clauses.
Pattern Switches :-They are used to make the pattern case-insensitive .
Property Description
I Ignore the case of the character.
g Global search for all occurences of a pattern.
gi Global search , ignore case.
Conclusion :-
From the above description , we can see that regular expression helps us a lot inthe situations like filling up validation form and restricts user from entering abnormal input and removes incompatibility between entered input and required input.
No comments:
Post a Comment