ruby的正则变量替换自己加标签em


You can also do this:
"a1".gsub(/(\w)(\d)/, '\2\1')
Which results in:
"1a"

"dog".gsub(/\w+/) { |animal| animal == "dog" ? "cat" : "dog" }


colors = {  "B" => "blue",  "G" => "green",  "R" => "red"}

"BBBGR".gsub(/\w/, colors)
bluebluebluegreenred



替换自己  \1 表示匹配的()第一个

==x.content.gsub(/(#{@word.name})/, '<em>\1</em>') 
阅读量: 49
发布于:
修改于: