У меня есть HTML-текст, где мне нужно заменить слова на ссылки на них. Например, у меня есть текст со словом «PHP», и вы хотите его заменить < a href = "glossary.html # php" > PHP </a >. И есть много слов, которые мне нужно заменить.Могу ли я использовать переменные в шаблоне в Regex (C#)
Мой код:!
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 никогда ничего не сделает. – diceguyd30