0

由于不是正则表达式的专家,我认为可以优化下一个代码。我将不胜感激任何帮助。

line = ".ui-icon-arrowrefresh-1-s { background-position: -176px -3px; }"

line.gsub!(/-?\d+px/) do |match|
  match.gsub(/-?\d+/) do |i|
    i.to_i + 4
  end
end

#=> ".ui-icon-arrowrefresh-1-s { background-position: -172px 1px; }"
4

2 回答 2

2
line.gsub(/-?\d+(?=px)/){|s| s.to_i+4}
于 2012-12-20T13:14:17.977 回答
2

你真的不需要任何前瞻或分组:

line.gsub(/-?\d+px/){|s| "#{s.to_i+4}px"}
#=> ".ui-icon-arrowrefresh-1-s { background-position: -172px 1px; }"
于 2012-12-20T13:47:27.590 回答