Expressions 1 2 – Play With Regular Expressions
- Expressions 1 2 – Play With Regular Expressions Quiz
- Expressions 1 2 – Play With Regular Expressions Problems
- Expressions 1 2 – Play With Regular Expressions Quiz
The legacy RegExp $1, $2, $3, $4, $5, $6, $7, $8, $9 properties are static and read-only properties of regular expressions that contain parenthesized substring matches.
Expressions 1 2 – Play With Regular Expressions Quiz
Description
Finite Automata and Regular Expressions A regular expression can be of the following forms: 1) R = P + Q 2) R = PQ 3) R = P. Where P and Q are also regular expressions. Assume that P and Q can be recognized by transition systems(FA) G and H respectively. This regular expression matches 10 digit US Phone numbers in different formats. Some examples are 1)area code in paranthesis. 2)space between different parts of the phone number. 3)no space between different parts of the number. 4)dashes between parts. 0-2 55 is a character class with three elements: the character range 0-2, the character 5 and the character 5 (again). This character class matches a single digit 0, 1, 2 or 5, just like 0125. Since regular expressions work with text, a regular expression engine treats 0 as a single character, and 255 as three characters. Two regular expressions can be altered or concatenated to form a new regular expression: if e 1 matches s and e 2 matches t, then e 1 e 2 matches s or t, and e 1 e 2 matches st. The metacharacters., +, and? Are repetition operators: e 1. matches a sequence of zero or more (possibly different) strings, each of which match e 1; e 1.
The $1, ..., $9 properties are static, they are not a property of an individual regular expression object. Instead, you always use them as RegExp.$1
, ..., RegExp.$9
.
The values of these properties are read-only and modified whenever successful matches are made.
The number of possible parenthesized substrings is unlimited, but the RegExp
object can only hold the first nine. You can access all parenthesized substrings through the returned array's indexes.
These properties can be used in the replacement text for the String.replace
method. When used this way, do not prepend them with RegExp
. The example below illustrates this. When parentheses are not included in the regular expression, the script interprets $n
's literally (where n
is a positive integer).
Examples
Using $n with String.replace
The following script uses the replace()
method of the String
instance to match a name in the format first last and output it in the format last, first. In the replacement text, the script uses $1
and $2
to indicate the results of the corresponding matching parentheses in the regular expression pattern.
Using $n with RegExp.test
The following script uses the test()
method of the RegExp
instance to grab a number in a generic string.
Expressions 1 2 – Play With Regular Expressions Problems
Please note that any operation involving the usage of other regular expressions between a re.test(str)
call and the RegExp.$n
property, might have side effects, so that accessing these special properties should be done instantly, otherwise the result might be unexpected.
Specifications
Browser compatibility
BCD tables only load in the browser