Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
A disjunction specifies multiple alternatives. Any alternative matching the input causes the entire disjunction to be matched.
One alternative pattern, composed of a sequence of atoms and assertions. Successfully matching one alternative causes the entire disjunction to be matched.
The | regular expression operator separates two or more alternatives. The pattern first tries to match the first alternative; if it fails, it tries to match the second one, and so on. For example, the following matches "a" instead of "ab", because the first alternative already matches successfully:
The | operator has the lowest precedence in a regular expression. If you want to use a disjunction as a part of a bigger pattern, you must group it.
When a grouped disjunction has more expressions after it, the matching begins by selecting the first alternative and attempting to match the rest of the regular expression. If the rest of the regular expression fails to match, the matcher tries the next alternative instead. For example,
This is because by selecting a in the first alternative, it's possible to select bc in the second alternative and result in a successful match. This process is called backtracking, because the matcher first goes beyond the disjunction and then comes back to it when subsequent matching fails.
Note also that any capturing parentheses inside an alternative that's not matched produce undefined in the resulting array.
An alternative can be empty, in which case it matches the empty string (in other words, always matches).
Alternatives are always attempted left-to-right, regardless of the direction of matching (which is reversed in a lookbehind).
The following example matches file extensions, using the same code as the input boundary assertion article:
| ECMAScript® 2027 Language Specification # prod-Disjunction |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.