JavaScript Regular Expressions 2 — Questions and Answers
Question 1: What does the `*` quantifier mean in a regular expression?
- Zero or more occurrences of the preceding token (Correct answer)
- One or more occurrences of the preceding token
- Zero or one occurrence of the preceding token
- Exactly one occurrence of the preceding token
Correct answer: Zero or more occurrences of the preceding token
`*` is a greedy quantifier that matches zero or more consecutive occurrences of the preceding element, making it optional.
Question 2: When `String.prototype.match()` is called with a regex that has the `g` flag, what does it return?
- An array containing all matched substrings (Correct answer)
- A single match object with index and groups
- A boolean indicating whether a match was found
- The index of the first match
Correct answer: An array containing all matched substrings
With the `g` flag, `match()` returns a flat array of all matched strings; without it, it returns a detailed array-like object for only the first match.
Question 3: What does the syntax `(?:...)` create in a regular expression?
- A non-capturing group that groups tokens without saving the match (Correct answer)
- A named capturing group
- A positive lookahead assertion
- A positive lookbehind assertion
Correct answer: A non-capturing group that groups tokens without saving the match
`(?:...)` creates a non-capturing group, which groups part of the pattern for quantifiers or alternation but does not save the matched text as a capture group.
Question 4: What does `\b` represent in a regular expression?
- A word boundary between a word character and a non-word character (Correct answer)
- A literal backspace character
- The beginning of the string
- Any blank (whitespace) character
Correct answer: A word boundary between a word character and a non-word character
`\b` is a zero-width assertion matching the position between a `\w` character and a `\W` character (or start/end of string), enabling whole-word matching.
Question 5: What does `RegExp.prototype.exec(str)` return when the pattern matches?
- An array containing the full match and any captured groups, with index and input properties (Correct answer)
- Only the matched substring as a plain string
- A boolean true value
- An array of all matches in the string
Correct answer: An array containing the full match and any captured groups, with index and input properties
`exec()` returns a result array with the full match at index 0, each capture group in subsequent indices, plus `index` and `input` properties — or `null` if no match.
Question 6: What does the quantifier `{2,5}` mean in a regular expression?
- Matches between 2 and 5 occurrences of the preceding token (Correct answer)
- Matches exactly 2 or exactly 5 occurrences
- Matches at least 2 but no maximum
- Matches 2 through 5 different characters
Correct answer: Matches between 2 and 5 occurrences of the preceding token
`{min,max}` is a range quantifier; `{2,5}` greedily matches the preceding element 2, 3, 4, or 5 times.
Question 7: What does the character class `[^abc]` match?
- Any single character that is NOT a, b, or c (Correct answer)
- The literal characters a, b, and c in sequence
- Any character at the start of the string
- The literal string '^abc'
Correct answer: Any single character that is NOT a, b, or c
Inside a character class `[...]`, a leading `^` negates it, so `[^abc]` matches any single character except the listed ones.
What does the `*` quantifier mean in a regular expression?