JavaScript Regular Expressions Basics Quiz

JavaScript
0 Passed
0% acceptance

Answer 35 questions covering regex literal vs constructor syntax, metacharacters, character classes, quantifiers, anchors, grouping, escaping, and JS testing/matching methods.

35 Questions
~70 minutes
1

Question 1

How do you declare a regex literal in JavaScript?

A
/pattern/flags
B
regex(pattern)
C
<pattern>
D
new Pattern()
2

Question 2

When is RegExp constructor syntax preferred?

A
When the pattern is built dynamically from strings at runtime
B
When the pattern never changes
C
When regex features are disabled
D
When you need automatic escaping
3

Question 3

What logs?

javascript
const literal = /cat/i
const dynamic = new RegExp('cat', 'i')
console.log(literal.test('CAT'))
console.log(dynamic.test('Cat'))
A
true true
B
false false
C
true false
D
false true
4

Question 4

What extra escaping is required when using the constructor?

A
Backslashes inside strings must be doubled (e.g., "\\d")
B
Dots must be tripled
C
Parentheses are disallowed
D
No escaping differences exist
5

Question 5

What does the dot (.) metacharacter match by default?

A
Any character except line terminators
B
Only digits
C
Only letters
D
Nothing; it is literal
6

Question 6

Which metacharacter matches a digit?

A
\d
B
\w
C
\s
D
\b
7

Question 7

What logs?

javascript
const regex = /a.c/
console.log(regex.test('abc'))
console.log(regex.test('ac'))
A
true false
B
false true
C
true true
D
false false
8

Question 8

What does \s match?

A
Whitespace characters (space, tab, newline, etc.)
B
Only spaces
C
Only newlines
D
Any non-whitespace character
9

Question 9

What does [aeiou] represent?

A
Any single lowercase vowel
B
Exactly the string "aeiou"
C
Any vowel sequence
D
Vowels followed by consonants
10

Question 10

How do you negate a character class?

A
[^...] places caret first to match anything not listed
B
[...]^ at end
C
Use double brackets [[...]]
D
Negation is impossible
11

Question 11

What logs?

javascript
const regex = /gr[ae]y/
console.log(regex.test('gray'))
console.log(regex.test('grey'))
console.log(regex.test('groy'))
A
true true false
B
true false true
C
false true false
D
false false true
12

Question 12

What does [^0-9] match?

A
Any character that is not a digit
B
Only digits
C
Digit sequences
D
Empty strings only
13

Question 13

What does the + quantifier mean?

A
One or more occurrences
B
Zero or one
C
Zero or more
D
Exactly one
14

Question 14

What logs?

javascript
const regex = /go+d/
console.log(regex.test('god'))
console.log(regex.test('good'))
console.log(regex.test('gd'))
A
true true false
B
false true true
C
true false true
D
false false true
15

Question 15

What does {3,5} mean when used as a quantifier?

A
Between three and five occurrences inclusive
B
Exactly five occurrences
C
At most three occurrences
D
Unlimited occurrences
16

Question 16

Why should you beware of overly greedy quantifiers?

A
They may consume more text than intended, requiring extra logic or making matches fail
B
They disable regex flags
C
They slow down string creation
D
They remove anchors
17

Question 17

What does ^ represent in regex?

A
Start of input (or line in multiline mode)
B
Literal caret character
C
End of input
D
Negation of a class
18

Question 18

What logs?

javascript
const regex = /^cat/
console.log(regex.test('catfish'))
console.log(regex.test('scatter' ))
A
true false
B
true true
C
false true
D
false false
19

Question 19

What does $ anchor?

A
End of input (or line in multiline mode)
B
Start of input
C
Word boundary
D
Decimal digits
20

Question 20

What logs?

javascript
const priceRegex = /\d+$/
console.log(priceRegex.test('Total: 20'))
console.log(priceRegex.test('20 USD'))
A
true false
B
false true
C
true true
D
false false
21

Question 21

Why use parentheses in regex?

A
To group patterns for quantifiers and capture matched substrings
B
To comment the regex
C
To escape special characters
D
To match literal parentheses only
22

Question 22

What logs?

javascript
const regex = /(ha)+!/ 
console.log(regex.test('hahaha!'))
console.log(regex.test('ha!'))
console.log(regex.test('ha'))
A
true true false
B
true false true
C
false true true
D
false false true
23

Question 23

How do non-capturing groups look?

A
(?:pattern)
B
(*pattern)
C
[pattern]
D
pattern?
24

Question 24

Why use non-capturing groups?

A
To group for alternation or quantifiers without populating capture groups
B
To speed up network requests
C
To auto-escape patterns
D
To enable lookbehind
25

Question 25

Which characters generally require escaping to match literally?

A
. ^ $ * + ? ( ) [ ] { } | \
B
Letters and digits
C
Whitespace only
D
No characters need escaping
26

Question 26

What logs?

javascript
const regex = /\./
console.log(regex.test('file.txt'))
console.log(regex.test('filetxt'))
A
true false
B
false true
C
true true
D
false false
27

Question 27

Why must you double escape backslashes in string-based regex creation?

A
String literals treat backslash as escape, so to pass \ to RegExp you write \\
B
To reduce file size
C
To enable unicode
D
To disable multiline mode
28

Question 28

What does regex.test(string) return?

A
Boolean indicating whether the pattern matches
B
Array of matches
C
The original regex
D
Number of captures
29

Question 29

Why might RegExp.prototype.exec be preferred over test?

A
exec returns detailed match info including capture groups and indexes
B
exec is faster for booleans
C
exec disables globals
D
exec automatically converts patterns to strings
30

Question 30

What logs?

javascript
const regex = /(\d+)-(\w+)/
const result = regex.exec('Order 24-shipping')
console.log(result[0])
console.log(result[1])
console.log(result[2])
A
'24-shipping', '24', 'shipping'
B
'Order', '24', 'shipping'
C
'24', 'shipping', undefined
D
null for all
31

Question 31

What does string.match(regex) return in ES2015 when regex has the g flag?

A
Array of all matches without capture groups
B
Single match array with groups
C
Boolean only
D
Iterator object
32

Question 32

What logs?

javascript
const matches = 'ababa'.match(/aba/g)
console.log(matches)
console.log(matches.length)
A
['aba'] and 1
B
['aba', 'aba'] and 2
C
null and 0
D
['ab'] and 1
33

Question 33

How does String.prototype.matchAll help?

A
It returns an iterator with all matches including capture groups, even with global flag
B
It returns only the first match
C
It mutates the string
D
It disables regex flags
34

Question 34

What logs?

javascript
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)
A
['cat']
B
['cats', 'cat', 'catalog']
C
['cat', 'cat']
D
[]
35

Question 35

Why reset lastIndex when reusing global regex with exec?

A
Because exec with g flag maintains state; without reset it continues from previous position
B
Because lastIndex stores flags
C
Because resetting increases speed
D
Because regex requires caching

QUIZZES IN JavaScript