匹配没有这些标签的任何标签中的任何文本
本文关键字:标签 任何 文本 | 更新日期: 2023-09-27 18:14:10
我正在尝试使用GeckoFX web浏览器控件实现一个简单的'查找页面'引擎(因为我不满意'window.find()',不能使任何其他工作。
这个想法是在包含搜索字符串的单元格或段落的innerhtml元素中添加" <span style='"background-color: gold;'">
搜索文本</span>
"格式。
当我在单元格中寻找匹配时。InnerText,如果我找到匹配,我想替换cell。innerhtml。如果细胞。InnerHtml包含标签内的搜索字符串,这些将被拧。
也许代码会更好地解释:这是我的输入字符串
<span><a href='"/some random link containing text'">test search text that should be found</a></span>
代码:string goldSpanStyle = "<span style='"background-color: gold;'">";
string textToFind = "text";
if (cell.TextContent.IndexOf(textToFind , comp) >= 0)
{
match = cell.TextContent.Substring(cell.TextContent.IndexOf(textToFind , stringComparisonOrdinalIgnoreCase), textToFind.Length);
}
if (match != "")
{
cell.InnerHtml = Regex.Replace(cellHtml, textToFind, goldSpanStyle + match + "</span>", RegexOptions.IgnoreCase);
}
现在,在这种情况下,我们将使用html,因为span格式也将添加到href属性中<span><a href='"/some random link containing <span style='"background-color: gold;'">text</span>'">test search <span style='"background-color: gold;'">text</span> that should be found</a></span>
我需要一个正则表达式,只匹配不在标签内的文本…我试过了(?!(<[^>]+>))(text)(?=<'/[^>]+>)
,但结果并不好,因为它只在搜索字符串的最后一个字母正好在结束标记(在这种情况下为'd')之前匹配(?!(<[^>]+>))test search text that should be found(?=<'/[^>]+>)
提前感谢您的帮助和建议Bartosz
= = =编辑:
基本上,我认为在像<a href="www.match.com">match</a>
这样的样本字符串中,我只需要匹配第二个"匹配"字,而不是<a href="www.match.com">
里面的那个…
下面的正则表达式将只捕获第二个test
或match
,
(test|match)(?=[^<>]*<)
演示