在我的代码中,我找到了所有匹配元素并用特殊值替换它。如何正确替换字符串
Regex imgRule = new Regex("img id=\\\".+?\\\"");
MatchCollection matches = imgRule.Matches(content.Value);
string result = null;
foreach (Match match in matches)
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
但是,我的代码是错误的,因为如果有不止一个匹配它仅替换最后一个,我怎么能纠正我的替换所有匹配文本代码?
什么是'content'类型?它在哪里宣布?节点的类型是什么?它在哪里宣布? –