RegExp pattern - alternation (Definition)

Sometimes you will want to match either one pattern or another using alternatives.

Availability:

ECMAScript edition - 3

You can group sub-expressions and offer them as alternatives. For example, you might want to match "AB" or "CD". The alternation operator can be placed between each sub-expression to indicate choices like this:

/AB|CD/

This technique can be used in combinations with character literals, classes and repetition operators. Like this:

/[a-z]{3}|[0-9]{40}/

This matches with three lower case letters or 40 numeric digits and nothing else will match.

See also:RegExp pattern, RegExp pattern - character class, RegExp pattern - character literal, RegExp pattern - repetition

Cross-references:

ECMA 262 edition 3 - section - 15.10.1

ECMA 262 edition 3 - section - 15.10.2.3

ECMA 262 edition 3 - section - 15.10.2.4