Skip to main content

Alternation (OR) |

Regular Expressions: Alternation (OR) |

What is Alternation in a regular expression?

View Answer:
Interview Response: Alternation is the term in a simple “OR” in a regular expression. In a regular expression, "OR” is denoted with a vertical line character |.

Code Example:

let regexp = /html|php|css|java(script)?/gi; // Alternation |

let str = 'First HTML appeared, then CSS, then JavaScript';

alert(str.match(regexp)); // 'HTML', 'CSS', 'JavaScript'