| JavaScript - Grouping and alternation | | Print | |
Grouping and alternation
Regular expressions use parentheses to group subexpressions, just as mathematical expressions do. Parentheses are useful, for example, to allow a repetition character to be applied to an entire subexpression. They are also useful with the | character, which is used to separate alternatives. Parenthesized groups have a special behavior: when a pattern match is found, the text that matches each group is saved and can be referred to by group number. The following table summarizes this syntax.
|
Character |
Meaning |
|---|---|
|
a | b |
Match either a or b |
|
(sub) |
Group subexpression sub into a single term, and remember the text that it matched |
|
(?:sub) |
Group subexpression sub but do not number the group or remember the text it matches (JS 1.5) |
|
\n |
Match exactly the same characters that were matched by group number n |
|
$n |
In replacement strings, substitute the text that matched the nth subexpression |
| Users' Comments (0) |
|
No comment posted






