Regular exp In JavaScript
javascript also provide Regular expressions that is very powerful tools for performing pattern matches.that is also used in PERL programmers and UNIX shell programmers .
An regular expression is written in the form of pattern or modifiers where pattern is the
regular expression itself and modifiers are a series of characters indicating various options. The "modifiers" part is optional.
There are characters which have special use in regular exps : [ \ ^ $ . | ? * + ( ).
There are Manely two methods for creating a RegExp object.
- RegExp Literal
- RegExp Object Constructor
Syntax :
var pattern = new RegExp(pattern, modifiers);
OR
var pattern = /pattern/modifiers;
Brackets are used to find a range of characters :
| Expression |
Description |
| [a-z] |
It matches any character from lowercase a through lowercase z. |
| [abc] |
Find any character between the brackets |
| [A-Z] |
It matches any character from uppercase A through uppercase Z. |
| [^abc] |
Find any character NOT between the brackets |
| (x|y) |
Find any of the alternatives specified |
| [0-9] |
It matches any decimal digit from 0 through 9. |
Modifiers used in reg() :
| Modifier |
Meaning |
Description |
| i |
Ignore Case |
its Perform a case-insensitive matching |
| g |
Global Search |
its Perform a global match that's find all matches rather than stopping after the first match. |
| m |
Multiline Input |
its Perform a multiline matching |
Regular Expression Quantifiers :
| Expression |
Description |
| * |
0 or more |
| + |
1 or more |
| ? |
0 or 1 |
| {2} |
Exactly 2 |
| {2, 5} |
Between 2 and 5 |
| {2,} |
2 or more |
Literal characters :
| Character |
Description |
| \0 |
The NUL character (\u0000) |
| \t |
Tab (\u0005) |
| \n |
Newline (\u000A) |
| \f |
Form feed (\u000C) |
| \r |
Carriage return (\u000D) |