开发者

Ignoring a character along with word boundary in regex

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
开发者_JS百科

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


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.


Be very careful with your \b boundaries. Here’s why.


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜