Ignoring a character along with word boundary in regex

十年热恋 提交于 2019-11-28 05:52:04

问题


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?


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/2902004/ignoring-a-character-along-with-word-boundary-in-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!