Bird
0
0

How can you use regex in Ruby to replace all vowels in a string with '*'?

hard📝 Application Q15 of 15
Ruby - Regular Expressions
How can you use regex in Ruby to replace all vowels in a string with '*'?
Astring.gsub(/[aeiou]/i, '*')
Bstring.replace(/[aeiou]/, '*')
Cstring.sub(/[aeiou]/, '*')
Dstring.match(/[aeiou]/).replace('*')
Step-by-Step Solution
Solution:
  1. Step 1: Identify method to replace all matches

    gsub replaces all occurrences matching regex in the string.
  2. Step 2: Use regex for vowels with case-insensitive flag

    /[aeiou]/i matches all vowels regardless of case.
  3. Step 3: Combine to replace vowels with '*'

    string.gsub(/[aeiou]/i, '*') replaces all vowels with '*'.
  4. Final Answer:

    string.gsub(/[aeiou]/i, '*') -> Option A
  5. Quick Check:

    Replace vowels = gsub with /[aeiou]/i [OK]
Quick Trick: Use gsub with /[aeiou]/i to replace all vowels [OK]
Common Mistakes:
  • Using sub replaces only first match
  • Using replace method incorrectly
  • Trying to call replace on match object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes