Bird
0
0

Which of the following is the correct syntax to check if the string "hello123" contains digits using the match operator?

easy📝 Syntax Q12 of 15
Ruby - Regular Expressions
Which of the following is the correct syntax to check if the string "hello123" contains digits using the match operator?
A"hello123" =~ /\d+/
B"hello123" =~ "\d+"
C"hello123" =~ /\w+/
D"hello123" =~ /[a-z]+/i
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct regex syntax

    The pattern to match digits is /\d+/, which is a regex literal in Ruby.
  2. Step 2: Match operator requires regex, not string

    The =~ operator expects a regex on the right side, so /\d+/ is correct, not a string.
  3. Final Answer:

    "hello123" =~ /\d+/ -> Option A
  4. Quick Check:

    Regex literal with =~ is correct [OK]
Quick Trick: Use slashes /pattern/ for regex with =~ operator [OK]
Common Mistakes:
  • Using a string instead of regex on right side
  • Confusing \d with \w for digits
  • Missing regex delimiters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes