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

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.

詳細については、 Ruby Regexを参照してください。

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

あなたがしたい:

gsub(/(ejb)/i, "<em>#{$1}</em>")
于 2013-04-24T03:08:58.043 に答える