如何使用正则表达式括号括起文本?

Zab*_*bba 2 ruby regex

我试图用带括号的正则表达式包围一些文本.例如,将所有内容替换is(is):

Input is      : This is a long sentence that IS written.
Desired output: This (is) a long sentence that (IS) written.
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?(虽然仍然保持找到的字符串的原始案例)

eph*_*ent 6

irb(main):001:0> s = 'This is a long sentence that IS written.'
=> "This is a long sentence that IS written."
irb(main):002:0> s.gsub(/\bis\b/i, '(\0)')
=> "This (is) a long sentence that (IS) written"
irb(main):003:0> s
=> "This is a long sentence that IS written"
irb(main):004:0> s.gsub!(/\bis\b/i, '(\0)')
=> "This (is) a long sentence that (IS) written"
irb(main):005:0> s
=> "This (is) a long sentence that (IS) written"
Run Code Online (Sandbox Code Playgroud)