我有一个字符串,如hello _there_.我想,以取代两个下划线<div>并</div>分别使用JavaScript的.输出(因此)看起来像hello <div>there</div>.该字符串可能包含多对下划线.
我所寻找的是一个方法来无论是运行在每场比赛,红宝石做它的方式的函数:
"hello _there_".gsub(/_.*?_/) { |m| "<div>" + m[1..-2] + "</div>" }
Run Code Online (Sandbox Code Playgroud)
或者能够引用匹配的组,再次以红宝石的方式完成:
"hello _there_".gsub(/_(.*?)_/, "<div>\\1</div>")
Run Code Online (Sandbox Code Playgroud)
任何想法或建议?