简单的正则表达式替换以保留原始字符串

发布于 2024-12-04 09:56:10 字数 597 浏览 0 评论 0原文

我有这个:

Title = Regex.Replace(Title, s, "<span style=\"background:yellow\">" + s + "</span>", RegexOptions.IgnoreCase);

其中 s 是像 facebook 这样的词。如果标题是:

How to make a Facebook game

我想替换为:

How to make a <span style="background:yellow">Facebook</span> game

即使搜索词是“facebook”(注意大写)。基本上,我如何保留单词的原始大写?

另一个例子,搜索词FACEBOOK,字符串Hello FaCeBoOk被转换为Hello FaCeBoOk

I have this:

Title = Regex.Replace(Title, s, "<span style=\"background:yellow\">" + s + "</span>", RegexOptions.IgnoreCase);

Where s is a word like facebook. If the title is:

How to make a Facebook game

I would like to replaced to:

How to make a <span style="background:yellow">Facebook</span> game

Even if the search word is 'facebook' (note capitalisation). Basically, how do I retain the original capitalisation of the word?

Another example, search term FACEBOOK, string Hello FaCeBoOk is turned to Hello <span style="background:yellow">FaCeBoOk</span>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

爱,才寂寞 2024-12-11 09:56:10

您可以使用 $& 替换 来实现:

Regex.Replace(Title, s, "<span style=\"background:yellow\">
amp;</span>", RegexOptions.IgnoreCase)

You can use the $& substitution for that:

Regex.Replace(Title, s, "<span style=\"background:yellow\">
amp;</span>", RegexOptions.IgnoreCase)
梦毁影碎の 2024-12-11 09:56:10

您可以简单地包含与单词“facebook”匹配的捕获组,并将该捕获组作为替换字符串的一部分包含在内。这最终会将其包含在最终结果中,与输入中显示的完全相同。

var title = "How to make a Facebook game";
title = Regex.Replace(title,
                      "(facebook)",
                      "<span style=\"background:yellow\">$1</span>",
                      RegexOptions.IgnoreCase);

查看实际操作

You can simply include a capture group that matches the word "facebook", and include that capture group as part of the replacement string. This will end up including it in the end result exactly as it appeared in the input.

var title = "How to make a Facebook game";
title = Regex.Replace(title,
                      "(facebook)",
                      "<span style=\"background:yellow\">$1</span>",
                      RegexOptions.IgnoreCase);

See it in action.

初与友歌 2024-12-11 09:56:10
var input = "How to make a Facebook game, do you like facebook?";
var searchFor = "facebook";
var result = Regex.Replace(
    input, 
    searchFor, 
    "<span style=\"background:yellow\">$+</span>", 
    RegexOptions.IgnoreCase);

唯一重要的是$+。它包含最后获取的文本。这甚至适用于“如何制作 Facebook 游戏,你喜欢 Facebook 吗?”第一个 Facebook 将保留大写,第二个 Facebook 将保留小写。

我要补充的是,如果您只想查找整个单词,那么您可以:

searchFor = @"\b" + searchFor + @"\b";

这将仅查找单词边界上的字符串。

var input = "How to make a Facebook game, do you like facebook?";
var searchFor = "facebook";
var result = Regex.Replace(
    input, 
    searchFor, 
    "<span style=\"background:yellow\">$+</span>", 
    RegexOptions.IgnoreCase);

The only important thing is the $+. It contains the last acquired text. This will work even for "How to make a Facebook game, do you like facebook?" The first Facebook will be left upper-case, the second one will be left lower-case.

I'll add that if you want to look only for whole words, then you can make:

searchFor = @"\b" + searchFor + @"\b";

This will look only for strings that are on word boundary.

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