Bruce Xinda Lin Bruce Xinda Lin - 5 months ago 14
Ruby Question

Get content between { } braces

How can I get the content in between "{ }" in Ruby? For example,


I love {you}


How can I fetch the element "you"? If I want to replace the content, say change "you" to "her", how should I do that? Probably using
gsub
?

Answer

Regular expressions are the way to go with gsub. Something like:

existingString.gsub(/\{(.*?)\}/) { "her" }
Comments