Ruby RegEx search and replace

This should be simple:

text = 'adam|paul|mike'

I want to replace that with   ->   <b>adam</b><b>paul</b><b>mike</b>

So each name seperated by a |  OR a name at the end of the string should be enclosed in <b> tags.  Here's what I have so far:

text.gsub(/[a-z]*[|\Z]/, '<b>\0</b>')

But this leaves off the last name.  I'd also like to replace [a-z]*  with just  .*  - (which for some reason is way off)

As a bonus, is there a way to also get the values inside the <b> tags to drop the  |    -  but that can always easily be done in a second step

Thanks!
ARACK04Asked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
use split to break the string in an array, change the diffrent items in place using collect and join the remainder array

text = 'adam|paul|mike'
arr = text.split('|')
arr.collect!{|a| '<b>' + a + '</b>' }
puts arr.join

easy, no?
0
 
Geert BormansInformation ArchitectCommented:
of course you can do this in one line
puts text.split('|').collect!{|a| '<b>' + a + '</b>' }.join
0
 
ARACK04Author Commented:
Not sure why I assumed I needed a regex here.  Thanks!.
0
All Courses

From novice to tech pro — start learning today.