| |
|
| Regular Expressions
1. PATTERNS | ^ | First char of expr. | ^a | expr beginning with a |
| $ | Final char of expr. | x$ | expr ending with x |
| . | Match any char. | a.b | expr with an a, then any char, then b |
| * | Match 0 or more occurrences of the preceding char. | 34* | a '3', followed by 0 or more 4's |
| + | Match 1 or more occurrences of the preceding char. | 34+ | a '3', followed by 1 or more 4's |
| ? | Match 0 or 1 occurrences of the preceding char. | aeiouy? | 'aeiou' or 'aeiouy' |
| \ | Escape special chars. | \? | An actual '?'. |
| [] | Any of a set of chars. | [abc] | expr containing 'a' or 'b' or 'c' |
| [-] | Specify a range. | [a-z] | expr containing any lowercase letter |
| [^] | Anything not in set of chars. | ^[^a-z]*$ | expr not containing any lowercase letters |
| \d | A digit – equivalent of [0-9] | \d\d | two consecutive digits |
| \w | A single alphanumeric char – or underscore. | #\w# | '#', then an alphanumeric char, then another '#' |
| \s | A single whitespace char – space, tab, form feed, line feed. | ^\s | expression beginning with white space |
| \r | Carriage return. | ^\r | expr beginning with carriage return |
| \n | Line feed. | \r\n | expr containing carriage return then line feed |
| () | Match and remember match – see next section. | (a) | expr containing the letter 'a' |
| a|b | Match a or b. Can be used with (). | (hi|hello) | expr containing the word 'hi' or the word 'hello' |
| {n} | Match n occurrences of pattern. | ^\d{5}$ | expr consisting of exactly 5 digits |
| {n,} | Match n or more occurrences of pattern. | ^\d{5,}$ | expr consisting of 5 or more digits |
| {n,m} | Match at least n and at most m occurrences of pattern. | ^\d{5,8}$ | expr consisting of 5-8 digits |
2. USAGE | Operations | Now that you know how to match patterns, what can you do with this knowledge? |
| search | You can search a string to see whether or not it contains a given pattern.
Typically, if the pattern exists, the return value will be the position of its first occurrence. |
| replace | You can search a string to see whether or not it contains a given pattern –
and if it exists, you can replace it with a new value – search and replace. |
| split | You can use a pattern as a delimiter for splitting a string into an array. |
| Parameters | You can indicate the type of pattern matching to use: |
| ignorecase | You can indicate (with 'i') that case is to be ignored in a pattern match. |
| global | You can indicate (with 'g') that every occurrence – not just the first –
should be sought. In general, applicable to replace, as opposed to search. |
| Regular Expression | This is an object that can be declared. Its properties are the pattern to be searched
for, as well as whether searches are global or case-sensitive. When is such an object useful? |
| language | In some languages, pattern-matching operations are methods of the regular expression (regexp)
object, so you simply have to declare one in order to carry out pattern-matching operations. |
| non-static values | Sometimes, you cannot use a string literal (like "abc") as the pattern to search
for, since you do not know in advance what pattern you will need to search for. Here, regexps are invaluable, as
they allow you to designate a variable as the pattern to search for. |
| Emphasis | There are three ways to invoke the pattern matching operations: |
| string | In this case, the various operations are called as methods of the string object, and
the pattern to search for (which can be a regular expression object)
is a parameter of the method. This is common in Javascript. |
| regexp | In this case, a 'Regular Expression' object is declared. The various operations are called
as methods of the regExp object, and
the string to search in is a parameter of the method. This is common in VBscript. |
| function | In this case, the pattern and the string to search for it are both arguments
of a function. This is common in PHP. |
| Referring back | When doing a search and replace operation, you may wish to refer back to part of the pattern
matched when replacing. For example, you may wish to replace a phone number in the
format '410-633-7788' with the area code. In such cases, you need to use two elements: |
| () | As mentioned in the "PATTERNS" section, surround the portion of the match that you wish to
'remember' with parentheses. So in our example, you would search for the pattern ^(\d{3}).*$.
This will match the entire phone number, but will 'remember' the first three digits. |
| $n | Next, when you indicate the pattern with which to replace the match, you would use $1, indicating
that you wish to replace the pattern matched with the first remembered pattern – in our case, the area code.
$2, $3, etc. can be used too. |
References: ??
Check this page for W3C Compliancy
Top of Page
|
|