忽略正则表达式中的字符和单词边界
我在 Ruby 中使用 gsub 将文本中的单词设为粗体。我使用单词边界,以免将其他单词中的字母加粗,但我发现这会忽略后面有引号的单词。例如:
text.gsub(/#{word}\b/i, "<b>#{word}</b>")
text = "I said, 'look out below'"
word = below
在这种情况下,下面的单词不会变成粗体。有什么方法可以忽略某些字符以及单词边界吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Regexp.new
中的所有转义看起来都非常难看。您可以通过使用 Regexp 文字来大大简化这一过程:此外,您可以直接使用
gsub!
的修饰符形式,除非该字符串在您未向我们展示的代码中的其他位置使用了别名。最后,如果您在gsub
调用中使用单引号字符串文字,则无需转义反斜杠。All that escaping in the
Regexp.new
is looking quite ugly. You could greatly simplify that by using a Regexp literal: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 yourgsub
call, you don't need to escape the backslash.非常小心您的
\b
边界。 原因如下 。Be very careful with your
\b
boundaries. Here’s why.#{word}
语法不适用于正则表达式。使用Regexp.new
代替:使用 sting 时,您需要将
\b
转义为\\b
,否则它会被解释为退格键。如果word
可能包含特殊的正则表达式字符,请使用Regexp.escape
对其进行转义。另外,通过将字符串替换为
#{word}
,您可以更改字符串的大小写:“BeloW”将替换为“below”。\0
通过替换为找到的单词来纠正此问题。另外,我在开头添加了\\b
,你不想查找“day”并以“sunday”。The
#{word}
syntax doesn't work for regular expressions. UseRegexp.new
instead:Note that when using sting you need to escape
\b
to\\b
, or it is interpreted as a backspace. Ifword
may contain special regex characters, escape it usingRegexp.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".