this is the closest answer I can come up with
gsub /(url([‘"]?)([^)’“]+)(['”]?))/, “\1#{”\2".capitalize}\3"
or
gsub /(url([‘"]?)([^)’“]+)(['”]?))/, “\1\2.capitalize\3”
but neither of these solutions work
It doen’t have to be with gsub, I just thought this would be the most
straightforward way
You can use following regexp:
string.gsub!(/(url([‘“]?)(.+?)([”’]?))/) { “#{$1}#{$2.upcase}#{$3}” }
It is similar to yours with two differences:
I’m using (.+?)
question mark here means that + is no longer greedy, it will match only
to the point where next element in regexp is detected: and in your case
that is either ", ’ or )
You cannot use functions on references like “\2.capitalize” or
“\2”.capitalize. It is just enterpreted as string. If you want to get
matches as actual string representations, you should use a block.
Although the last example by w_a_x_man looks efficient and simpler, I
needed a very specific match, as it actually matching
(http://www.google.com) and not even considering the url, then just
trying to capitalize the parentheses.
I went with
“url(http://www.google.com)”.sub /(url([‘"]?)([^)’“]+)(['”]?))/,
“#{$1}#{$2.upcase}#{$3}”