JavaScript Regular Expressions Basics Quiz
Answer 35 questions covering regex literal vs constructor syntax, metacharacters, character classes, quantifiers, anchors, grouping, escaping, and JS testing/matching methods.
Question 1
How do you declare a regex literal in JavaScript?
Question 2
When is RegExp constructor syntax preferred?
Question 3
What logs?
const literal = /cat/i
const dynamic = new RegExp('cat', 'i')
console.log(literal.test('CAT'))
console.log(dynamic.test('Cat'))Question 4
What extra escaping is required when using the constructor?
Question 5
What does the dot (.) metacharacter match by default?
Question 6
Which metacharacter matches a digit?
Question 7
What logs?
const regex = /a.c/
console.log(regex.test('abc'))
console.log(regex.test('ac'))Question 8
What does \s match?
Question 9
What does [aeiou] represent?
Question 10
How do you negate a character class?
Question 11
What logs?
const regex = /gr[ae]y/
console.log(regex.test('gray'))
console.log(regex.test('grey'))
console.log(regex.test('groy'))Question 12
What does [^0-9] match?
Question 13
What does the + quantifier mean?
Question 14
What logs?
const regex = /go+d/
console.log(regex.test('god'))
console.log(regex.test('good'))
console.log(regex.test('gd'))Question 15
What does {3,5} mean when used as a quantifier?
Question 16
Why should you beware of overly greedy quantifiers?
Question 17
What does ^ represent in regex?
Question 18
What logs?
const regex = /^cat/
console.log(regex.test('catfish'))
console.log(regex.test('scatter' ))Question 19
What does $ anchor?
Question 20
What logs?
const priceRegex = /\d+$/
console.log(priceRegex.test('Total: 20'))
console.log(priceRegex.test('20 USD'))Question 21
Why use parentheses in regex?
Question 22
What logs?
const regex = /(ha)+!/
console.log(regex.test('hahaha!'))
console.log(regex.test('ha!'))
console.log(regex.test('ha'))Question 23
How do non-capturing groups look?
Question 24
Why use non-capturing groups?
Question 25
Which characters generally require escaping to match literally?
Question 26
What logs?
const regex = /\./
console.log(regex.test('file.txt'))
console.log(regex.test('filetxt'))Question 27
Why must you double escape backslashes in string-based regex creation?
Question 28
What does regex.test(string) return?
Question 29
Why might RegExp.prototype.exec be preferred over test?
Question 30
What logs?
const regex = /(\d+)-(\w+)/
const result = regex.exec('Order 24-shipping')
console.log(result[0])
console.log(result[1])
console.log(result[2])Question 31
What does string.match(regex) return in ES2015 when regex has the g flag?
Question 32
What logs?
const matches = 'ababa'.match(/aba/g)
console.log(matches)
console.log(matches.length)Question 33
How does String.prototype.matchAll help?
Question 34
What logs?
const regex = /\bcat\b/g
const str = 'cats catalog cat'
let match
const found = []
while ((match = regex.exec(str)) !== null) {
found.push(match[0])
}
console.log(found)Question 35
Why reset lastIndex when reusing global regex with exec?
