本文介绍了gsub 部分替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想替换此表达式中括号中的组:
I would like to replace only the group in parenthesis in this expression :
my_string.gsub(/<--MARKER_START-->(.)*<--MARKER_END-->/, 'replace_text')
所以我得到:<--MARKER_START-->replace_text<--MARKER_END-->
我知道我可以在替换表达式中重复整个 MARKER_START
和 MARKER_END
块,但我认为应该有一种更简单的方法来做到这一点.
I know I could repeat the whole MARKER_START
and MARKER_END
blocks in the substitution expression but I thought there should be a more simple way to do this.
推荐答案
你可以这样做:
my_string.gsub(/(<--MARKER_START-->)(.*)(<--MARKER_END-->)/, '\1replace_text\3')
这篇关于gsub 部分替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!