Redmine plugin that replaces words via regular expression?
I'm very new to Redmine/Ruby trying to achieve a simple plugin that takes the current 开发者_如何学JAVAwiki page content and matches/replaces everytime a word occurs via regular expression. How can I do this?
Thanks!
Dennis
The word replacement can de done by using gsub()
with \b
to match a word boundary:
irb(main):001:0> 'foo bar baz foo bar'.gsub /\bfoo\b/, 'replaced'
=> "replaced bar baz replaced bar"
Here is a more complete solution with a dictionary of words to replace:
repl = {'foo'=>'apple', 'baz'=>'banana'}
s = 'foo bar baz foo bar'
for from, to in repl:
s = s.gsub /\b#{from}\b/, to
end
Result: apple bar banana apple bar
精彩评论