我有一个字符串,例如hello _there_
. 我想使用JavaScript分别用<div>
和替换两个下划线。输出(因此)看起来像. 该字符串可能包含多对下划线。</div>
hello <div>there</div>
我所寻找的是一个方法来无论是运行在每场比赛,Ruby做它的方式的函数:
"hello _there_".gsub(/_.*?_/) { |m| "<div>" + m[1..-2] + "</div>" }
或者能够引用匹配的组,同样可以在 ruby 中完成:
"hello _there_".gsub(/_(.*?)_/, "<div>\\1</div>")
有什么想法或建议吗?