JavaScript Regular Expressions 1 — Questions and Answers
Question 1: Which method tests whether a regular expression matches a string and returns a boolean?
- regex.test(str) (Correct answer)
- str.match(regex)
- regex.exec(str)
- str.search(regex)
Correct answer: regex.test(str)
The `test()` method on a RegExp object returns `true` if the pattern matches and `false` otherwise.
Question 2: What does the `g` flag do when added to a regular expression like `/abc/g`?
- Finds all matches instead of stopping after the first (Correct answer)
- Makes matching case-insensitive
- Enables multiline mode
- Makes `.` match newline characters
Correct answer: Finds all matches instead of stopping after the first
The `g` (global) flag causes methods like `match()` and `replace()` to process all occurrences in the string rather than just the first.
Question 3: Which character class shorthand matches any single digit (0–9)?
- \d (Correct answer)
- \w
- \s
- \n
Correct answer: \d
`\d` is a shorthand character class equivalent to `[0-9]` and matches any single numeric digit.
Question 4: What does `^` mean when placed at the very start of a regex pattern (outside a character class)?
- Asserts the match must start at the beginning of the string (Correct answer)
- Negates the entire pattern
- Matches zero or more of the preceding token
- Matches any character except a newline
Correct answer: Asserts the match must start at the beginning of the string
Outside of a character class, `^` is an anchor that asserts the position is at the start of the string (or line in multiline mode).
Question 5: What does `String.prototype.replace(regex, newValue)` return when passed a regular expression?
- A new string with the matched portion replaced (Correct answer)
- The number of replacements made
- The original string (strings are immutable)
- An array of replacement results
Correct answer: A new string with the matched portion replaced
`replace()` returns a new string where matches of the regex are substituted with the replacement value; the original string is not mutated.
Question 6: What does the shorthand `\w` match in a regular expression?
- Any alphanumeric character or underscore ([A-Za-z0-9_]) (Correct answer)
- Any whitespace character
- Any word at a word boundary
- Any non-word character
Correct answer: Any alphanumeric character or underscore ([A-Za-z0-9_])
`\w` is equivalent to `[A-Za-z0-9_]` and matches letters, digits, and underscores — commonly called 'word characters'.
Question 7: What does the `i` flag do in a JavaScript regular expression?
- Makes the pattern match regardless of letter case (Correct answer)
- Enables global matching across the string
- Enables multiline mode for `^` and `$`
- Enables the sticky matching mode
Correct answer: Makes the pattern match regardless of letter case
The `i` (case-insensitive) flag causes the regex engine to treat uppercase and lowercase letters as equivalent during matching.
Which method tests whether a regular expression matches a string and returns a boolean?