Regular Expression are very important in professional JavaScript.
There is a wonderful
Resource in Mozilla Developer Center.
Two ways to construct a regular expression
1. Literal expression
Re =/AB + C/g;
2. constructor Function
Re = new Regexp ("AB + C", "G ");
Possible flags
1. G-global search.
2. I-case-insensitive search.
3. M-multi-line search.
Simple and special characters
A regular expression is composed of simple characters (such as/ABC123 _/) and special
Characters.
Some important special characters:
1. ^-matches beginning of input.
2. $-matches end of input.
3. *-matches the preceding character 0 or more times.
4. +-matches the preceding character 1 or more times.
5 .? -Matches the preceding character 0 or 1 time.
6 ..-matches any single character.
Sepcial character \
There are two meanings of the special charater.
1. used before simple character, indicating that the next character is special.
E.g. B is a simple character, \ B is used to indicate a word
Boundary.
Some important special character \:
1. \ B-matches a word boundary.
2. \ D-matches a digit character.
3. \ s-matches a white space character, including space,
Tab, line feed.
4. \ W-matches any alphanumeric character including
Underscore. equivalent to [A-Za-z0-9 _].
Javascript funtions to operate regular expression
1. Exec-returns: array.
2. Match-returns: array or null.
3. Test-returns: true or false.
4. Search-returns: Index or-1.
5. replace-returns: string.
Examples:
/D (B +) D/g.exe C ("cdbbdbsbz"); // ["dbbd", "BB"] "cdbbdbsbz ". match (/d (B +) D/g); // ["dbbd"] "cdbbdbsbz ". search (/d (B +) D/g); // 1/D (B +) D/g. test ("cdbbdbsbz"); // true