繁体   English   中英

匹配没有这些标签的任何标签内的任何文本

[英]Match any text inside any tags without these tags

我正在尝试使用GeckoFX webbrowser控件实现一个简单的“在页面上查找”引擎(因为我对'window.find()'不满意,并且无法使其他任何工作。

我们的想法是将“ <span style=\\"background-color: gold;\\">搜索文本</span> ”格式添加到包含搜索字符串的单元格或段落的innerhtml元素中。

当我在cell.InnerText中查找匹配项时,如果找到匹配项,我想替换cell.InnerHtml。 如果cell.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">match</a>我只需匹配第二个“匹配”字,而不是<a href="www.match.com"> ...

以下正则表达式只捕获第二次testmatch

(test|match)(?=[^<>]*<)

DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM