忽略正则表达式中的字符和单词边界

发布于 2024-09-03 00:08:55 字数 255 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除→记忆 2024-09-10 00:08:55

Regexp.new 中的所有转义看起来都非常难看。您可以通过使用 Regexp 文字来大大简化这一过程:

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

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

此外,您可以直接使用 gsub! 的修饰符形式,除非该字符串在您未向我们展示的代码中的其他位置使用了别名。最后,如果您在 gsub 调用中使用单引号字符串文字,则无需转义反斜杠。

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.

暗藏城府 2024-09-10 00:08:55

非常小心您的\b边界。 原因如下

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

情话已封尘 2024-09-10 00:08:55

#{word} 语法不适用于正则表达式。使用 Regexp.new 代替

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

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

:使用 sting 时,您需要将 \b 转义为 \\b,否则它会被解释为退格键。如果word可能包含特殊的正则表达式字符,请使用Regexp.escape对其进行转义。

另外,通过将字符串替换为 #{word},您可以更改字符串的大小写:“BeloW”将替换为“below”。 \0 通过替换为找到的单词来纠正此问题。另外,我在开头添加了\\b,你不想查找“day”并以“sunday”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文