0

我有以下字符串。

x = %q{ On the server side, my EJB3 application uses Spring for configuring all sorts of things. So my EJBs all look up various ApplicationContexts and use them as a sort of...well, I was going to say poor man's JNDI, but the reality is that JNDI in the J2EE environment is really a poor man's Spring. :-)

On the GUI side, I use it instead of resource injection to get access to my EJBs. That lets me test the GUI component with simple pojos. So ejb is very good technology}

我正在替换字符串"ejb",不区分大小写。我正在这样做:

 y = x.gsub(/(ejb)/i, '<em>EJB</em>')
 # => " On the server side, my <em>EJB</em>3 application uses Spring for configuring all sorts of things. So my <em>EJB</em>s all look up various ApplicationContexts and use them as a sort of...well, I was going to say poor man's JNDI, but the reality is that JNDI in the J2EE environment is really a poor man's Spring. :-)\n\nOn the GUI side, I use it instead of resource injection to get access to my <em>EJB</em>s. That lets me test the GUI component with simple pojos. So <em>EJB</em> is very good technology"

我有一个小写匹配: "ejb",我用这个替换: "<em>EJB</em>"。如何在不更改大小写的情况下更换它?我想要"<em>ejb</em>"

4

4 回答 4

3
x.gsub(/(ejb)/i, '<em>\1</em>')
于 2013-04-24T05:28:32.403 回答
0

你要:

gsub(/(ejb)/i, "<em>#{$1}</em>")
于 2013-04-24T03:08:58.043 回答
0
'test EjB123'.gsub(/ejb/i, "<em>#{$1}</em>")
 => "test <em>EjB</em>123" 
于 2013-04-24T03:24:30.430 回答
0

您可能想尝试:

x.gsub!(/(ejb)/i) {|m| "<em>#{$~}<em>"}

有一Special global variables组由Pattern matching:

$~ is equivalent to ::last_match;
$& contains the complete matched text;
$` contains string before match;
$' contains string after match;
$1, $2 and so on contain text matching first, second, etc capture group;
$+ contains last capture group.

欲了解更多信息: 红宝石正则表达式

于 2013-04-24T03:26:45.310 回答