Gsub regex


I’m trying to convert this:
To this:
With this piece of code:
BUT I want to avoid having a space appear before the dash and I’ve tried to search how to do this but to no avail.
: (


You can simply just use another regex
x = "Haunted-House"
x.gsub(/[A-Z\s]/, "_ ")
“_ _ _ _ _ _ _ _ -_____”
x.gsub(/\s[-]/, "-")
Will give you the output you want

I recommend using this tool for regex matching


I tried using both regexes and it seems that the ‘H’ in ‘HOUSE’ has been pushed over by an underscore and the letter ‘E’ is no longer available as it was also pushed over completely.
Here is the outcome:

Btw, I want to thank you for helping me! : )

I am very new to regex and still have not touched the topic in my studies but I decided to get a taste of it now!

Just a heads up, this is for my hangman game!




y = x.gsub(/[A-Z\s]/, "_ ")

=> "_ _ _ _ _ _ _ -_ _ _ _ _ "

z = y.gsub(/\s[-]/,"-")

Codecademy is mangling the output but z will output what you want