The scan method expects a regex pattern, not a plain string.
Step 2: Identify correct regex usage
The pattern should be written as /a\w+/ to match words starting with 'a'.
Final Answer:
The pattern should be a regex, not a string. -> Option A
Quick Check:
scan requires regex pattern, not string [OK]
Hint: Use /pattern/ for regex in scan, not string quotes [OK]
Common Mistakes:
Passing string instead of regex to scan
Misunderstanding backslash escaping
Assuming scan modifies the string
5. Given the string data = "log12 ID123, ID456, ID789", how can you use scan to extract only the numeric parts after each 'ID' (e.g., ["123", "456", "789"])?
hard
A. data.scan(/ID(\d+)/).flatten
B. data.scan(/\d+/)
C. data.scan(/ID\d+/)
D. data.scan(/(ID\d+)/)
Solution
Step 1: Understand the pattern to extract numbers after 'ID'
The regex /ID(\d+)/ matches 'ID' followed by digits, capturing only digits in parentheses. This ignores other digits like '12' in 'log12'.
Step 2: Use scan and flatten the result
scan returns an array of arrays for capture groups, so flatten converts it to a simple array of strings.
Final Answer:
data.scan(/ID(\d+)/).flatten -> Option A
Quick Check:
Capture group extracts digits, flatten simplifies array [OK]
Hint: Use capture groups and flatten to get inner matches [OK]
Common Mistakes:
Using data.scan(/\d+/) which grabs all digits including extras like '12' and '78'
Using scan without flatten, getting nested arrays
Matching full 'ID123' instead of just digits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.