I want to replace all dashes that are inside square brackets but leave the ones that aren't.
String: dont-change-this[only-change-inside-brackets]
Result: dont-change-this[only_change_inside_brackets]
The way I'm currently doing it is by capturing everything in the square brackets and then replacing.
regex = /(\[([a-z-]+)\])/
testString = "dont-change-this[only-change-inside-brackets]"
testString.match regex
testString.sub(regex, $1.gsub(/-/, '_'))
It works, but I was wondering if there is a way to do this in just one expression.