Alternation (OR) |
Regular Expressions: Alternation (OR) |
What is Alternation in a regular expression?
View Answer:
let regexp = /html|php|css|java(script)?/gi; // Alternation |
let str = 'First HTML appeared, then CSS, then JavaScript';
console.log(str.match(regexp)); // 'HTML', 'CSS', 'JavaScript'
How is alternation denoted in Regex?
View Answer:
Can you explain how Regex processes alternation?
View Answer:
Can Regex alternation match multiple patterns simultaneously?
View Answer:
What's the difference between character classes and alternation?
View Answer:
Character Classes:
let str1 = "gray";
let str2 = "grey";
let regex = /gr[ae]y/;
console.log(regex.test(str1)); // true
console.log(regex.test(str2)); // true
In this example, gr[ae]y
is a regular expression that matches either "gray" or "grey". The character class [ae]
matches either 'a' or 'e'.
Alternation:
let str1 = "I have a cat";
let str2 = "I have a dog";
let regex = /cat|dog/;
console.log(regex.test(str1)); // true
console.log(regex.test(str2)); // true
In this example, cat|dog
is a regular expression that matches either "cat" or "dog". The alternation operator |
allows to match either the pattern on its left or the pattern on its right.
Remember, while both character classes and alternation can be used to match one of multiple patterns, character classes are limited to matching one character out of several possibilities, while alternation can be used to match one of multiple more complex patterns.
It's worth noting that alternation has a higher computational cost than character classes because it requires more processing power to check each alternative, especially if there are multiple options. So, it's more efficient to use character classes for single characters, and alternation for longer or more complex patterns.
Can alternation be used in combination with other Regex operations?
View Answer:
let str = "I have 4 cats and 3 dogs";
let regex = /\b(cats|dogs)\b/g;
let match;
while ((match = regex.exec(str)) != null) {
console.log(`Matched: ${match[0]}`); // prints 'cats', then 'dogs'
}
In this regex, (cats|dogs)
is a group that matches either 'cats' or 'dogs'. The \b
on either side of the group is a word boundary, ensuring that we match 'cats' or 'dogs' as whole words rather than parts of other words.
Here's another example that uses alternation within a group, combined with a quantifier:
let str = "123-456-7890";
let regex = /^(\d{3}-){2}\d{4}$/;
console.log(regex.test(str)); // true
In this regex, (\d{3}-){2}
matches a sequence of three digits followed by a hyphen, repeated exactly twice. This is combined with \d{4}
to match a sequence of four digits. This regular expression is designed to match a phone number in the format '123-456-7890'. The ^
and $
are anchors indicating the start and end of the string, ensuring that the whole string conforms to the pattern.
Is there any downside to using alternation excessively in Regex?
View Answer:
How does Regex handle non-matching alternatives?
View Answer:
How is precedence determined in Regex alternation?
View Answer:
Can you have an alternation with no alternatives?
View Answer:
How does alternation behave with empty strings?
View Answer:
Can alternation be nested within a Regex pattern?
View Answer:
let str = "I love cats";
let regex = /I love (cats|dogs|(small|big) birds)/;
console.log(regex.test(str)); // true
In this example, the regular expression I love (cats|dogs|(small|big) birds)
matches the string "I love " followed by either "cats", "dogs", or "small birds" or "big birds". The expression (small|big) birds
is a nested alternation.
It's important to note that when nesting alternations like this, you should be aware of how the regular expression engine processes the pattern. The engine will attempt to match the alternates from left to right. Once it finds a match, it stops and doesn't check the remaining alternates. So the order can sometimes matter, especially when dealing with more complex patterns.
What's the impact of using grouping with alternation in Regex?
View Answer:
Here's an example to illustrate:
let str = "I love cats";
let regex = /(I love cats)|(I love dogs)/;
console.log(regex.test(str)); // true
In this example, (I love cats)|(I love dogs)
matches either the entire string "I love cats" or the entire string "I love dogs". If a match is found, you can also retrieve the matched string:
let match = regex.exec(str);
if (match != null) {
console.log(`Matched: ${match[0]}`); // Matched: I love cats
}
In this case, match[0]
contains the entire matched string, while match[1]
and match[2]
contain the matched strings for the first and second groups, respectively. If the match for a group is not found, the corresponding entry in the match array will be undefined
.
How does quantification work with alternation in Regex?
View Answer:
let str = "cataaaa";
let regex = /(cat|caaaa)t/;
console.log(regex.test(str)); // false
In this example, we might expect the regex to match the string "cataaaa", because "caaaa" is one of the alternatives in the group. However, the match fails because the regex engine is greedy. It first matches "cat", leaving only "aaaa", which does not match the "t" at the end of the regex.
If you want to match either "cat" or "caaaa", followed by a "t", you need to adjust the regular expression to avoid the greediness. One option is to make the alternatives mutually exclusive:
let regex = /(caaaa|cat)t/;
console.log(regex.test(str)); // true
In this example, "caaaa" is checked before "cat", so the regex engine matches "caaaa", leaving "t", which matches the "t" at the end of the regex.
Remember that the order of alternatives can be important when using quantifiers with alternation, due to the greediness of the regex engine.
An important thing to note is that quantifiers in JavaScript regular expressions are greedy by default. This means that they will match as many instances of the pattern as possible. This behavior can sometimes lead to unexpected results when used with alternation.