سؤال

I'm very new to Redmine/Ruby trying to achieve a simple plugin that takes the current wiki 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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top