What does "9876543210".replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3") return?
Each $n inserts the text captured by the nth group: 987, 654 and 3210.
String Methods · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript string methods: replace and replaceAll, padStart, trim, split with limits, match and matchAll, search, includes positions, localeCompare, normalize, regex flags and lastIndex. Pick an option to check your answer instantly.
What does "9876543210".replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3") return?
Each $n inserts the text captured by the nth group: 987, 654 and 3210.
What does "banana".includes("ban", 1) return?
The second argument is where the search begins. "anana" contains no "ban".
What do the flags in /error/gi mean?
Other common flags: m (multi-line ^ and $), s (dot matches newlines), u (Unicode) and y (sticky).
Why can const re = /\d/g; make re.test("a1") return true and then false on the next call with the same string?
After the first match lastIndex is 2, so the second call searches from index 2, finds nothing and resets lastIndex to 0. Do not use g for simple yes/no tests.
Which method returns true or false?
startsWith, endsWith and includes return booleans. indexOf and search return positions (-1 when missing) and match returns an array or null.
Timed online tests with instant scores and explanations.