【发布时间】:2010-11-05 01:48:53
【问题描述】:
我有一些 HTML 文本,我需要将单词替换为它们上的链接。例如,我有一个带有单词“PHP”的文本,并且想用 PHP 替换它。还有很多词需要替换。
我的代码:
public struct GlossaryReplace
{
public string word; // here the words, e.g. PHP
public string link; // here the links to replace, e.g. glossary.html#php
}
public static GlossaryReplace[] Replaces = null;
IHTMLDocument2 html_doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
string html_content = html_doc.body.outerHTML;
for (int i = 0; i < Replaces.Length; i++)
{
String substitution = "<a class=\"glossary\" href=\"" + Replaces[i].link + "\">" + Replaces[i].word + "</a>";
html_content = Regex.Replace(html_content, @"\b" + Replaces[i].word + "\b", substitution);
}
html_doc.body.innerHTML = html_content;
问题是 - 这不起作用:(但是,
html_content = Regex.Replace(html_content, @"\bPHP\b", "some replacement");
这段代码运行良好!我无法理解我的错误!
【问题讨论】:
-
您永远不会为“替换”分配任何内容,因此您的 for 循环将永远不会做任何事情。