tags:

views:

92

answers:

2

I am using gsub in Ruby to make a word within text bold. I am using a word boundary so as to not make letters within other words bold, but am finding that this ignores words that have a quote after them. For example:

text.gsub(/#{word}\b/i, "<b>#{word}</b>")

text = "I said, 'look out below'"
word = below

In this case the word below is not made bold. Is there any way to ignore certain characters along with a word boundary?

A: 

The #{word} syntax doesn't work for regular expressions. Use Regexp.new instead:

word = "below"
text = "I said, 'look out below'"

reg = Regexp.new("\\b#{word}\\b", true)
text = text.gsub(reg, "<b>\\0</b>")

Note that when using sting you need to escape \b to \\b, or it is interpreted as a backspace. If word may contain special regex characters, escape it using Regexp.escape.

Also, by replacing the string to <b>#{word}</b> you may change casing of the string: "BeloW" will be replaced to "below". \0 corrects this by replacing with the found word. In addition, I added \\b at the beginning, you don't want to look for "day" and end up with "sunday".

Kobi
This is a proud moment for me - my first ever Ruby! I tested the code using http://www.ruby.ch/interpreter/rubyinterpreter.shtml , and referred the apparently useful docs.
Kobi
Ok thanks. However, I am using Ruby, and would rather stick to using the gsub method. I use #{word} to pass the word that I am trying to highlight. So in this example, word = 'below'
DavidP6
+1  A: 

All that escaping in the Regexp.new is looking quite ugly. You could greatly simplify that by using a Regexp literal:

word = 'below'
text = "I said, 'look out below'"

reg = /\b#{word}\b/i
text.gsub!(reg, '<b>\0</b>')

Also, you could use the modifier form of gsub! directly, unless that string is aliased in some other place in your code that you are not showing us. Lastly, if you use the single quoted string literal inside your gsub call, you don't need to escape the backslash.

Jörg W Mittag