kernification kernification - 5 months ago 26
Ruby Question

Using wildcards in gsub

I woudl like to do something like this in Ruby on Rails:

"String with text and $abc$ and $def$ and so on... ".gsub("$*$", "replaceText")


results in

"String with text and replaceText and replaceText and so on... "


But I have no idea how wildcards like "*" works here.

Answer

You can use regex for this purpose

"String with text and $abc$ and $def$ and so on... ".gsub(/\$\w+\$/, "replaceText")
#=> "String with text and replaceText and replaceText and so on... "

Remember you will need to escape $

Comments