|
LILLY ROBOT |
A regular expression object describes a pattern of characters, which can be matched agaist strings.
var name= /pattern of characters/
In the following text we are going to use a regular expression with the method
match() to find the word apple.
Displays
You can use the regular expressions without declaring a variable - let us find the word orange.
Displays
| Modifiers | Explanation |
|---|---|
| g | used for a global match - finds all the matches within the string | i | used for case-insensitive matching |
| m | used for searching across multiple lines of text |
Displays
Displays
The tables below show some of the common regular expressions that are used.
| Expression | Explanation |
|---|---|
| \d | matches a single numberic character |
| \D | matches a single character that is not a numeric chracter |
| \n | matches a new line |
| \t | matches a tab |
| \s | matches a white space character |
| \d | matches a digit |
| \w | matches any single character that is a letter, number, or underscore |
| \f | matches a form feed |
| \t | matches a tab |
| \r | matches a carriage return |
| \b | matches a word boundary - the start or end of a word | \B | matches a position that is not located at a word boundary |
Displays
| Metacharacter | Explanation |
|---|---|
| [0-9] | matches any digits from 0 through to 9 |
| [a-z] | matches any lowercase characters from a through to z |
| [A-Z] | matches any uppercase characters from A through to Z |
| ^ | matches character from the begining of a line |
| $ | matches characters from the end of a line |
| . | matches any single character except line break characters |
Displays
| Methods | Explanation |
|---|---|
| * | x* matches any string containing zero or more x's - it can be any characters |
| + | x+ must match any string containing at least one x - it can be any characters |
| ? | x? matches any string containing zero or one x - it can be any characters |
| {n} | x{2} matches any string containing a sequence of 2 x's |
| {n,n1} | x{5, 6} matches any string containing a sequence of five or six x's |
Displays