Bird
0
0

Which of the following is the correct syntax for a regex literal that matches the word "hello" in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Regular Expressions
Which of the following is the correct syntax for a regex literal that matches the word "hello" in Ruby?
A/'hello'/
B"/hello/"
C/hello/
D/hello
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct regex literal syntax

    Regex literals in Ruby are enclosed between forward slashes, e.g., /pattern/.
  2. Step 2: Evaluate options

    /hello/ correctly uses /hello/. "/hello/" is a string containing regex literal, not a regex. /'hello'/ includes quotes inside regex which matches quotes literally. /hello is missing the closing slash.
  3. Final Answer:

    /hello/ -> Option C
  4. Quick Check:

    Regex literal syntax = /pattern/ [OK]
Quick Trick: Regex literals must start and end with slashes /pattern/ [OK]
Common Mistakes:
  • Using quotes around regex literal
  • Omitting closing slash
  • Including quotes inside regex unintentionally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes