如何在 html 字符串中用 ~/ 替换 http://locallhost

我想从下面的 C# 代码中替换"http://localhost:59455/"之前的每一个。"Images/TestFiles/(file name)"

string tags = @"<p><img class='img - fluid' src='http://localhost:59455/Images/TestFiles/1.JPG'></p><p><br></p><p><img class='img-fluid' src='http://localhost:59455/Images/TestFiles/2.JPG'></p>";
string final = Regex.Replace(tags, "http.*/Images", "~/Images");

但它总是给我错误的结果,如下所示:

<p><img class='img - fluid' src='~/Images/TestFiles/2.JPG'></p>

虽然我期待这样的结果:

<p><img class='img - fluid' src='~/Images/TestFiles/1.JPG'></p><p><br></p><p><img class='img-fluid' src='~/Images/TestFiles/2.JPG'></p>

你可以看到,它只替换了一个。


慕桂英4014372
浏览 108回答 1
1回答

开心每一天1111

*是贪心的,匹配从第一个http到最后的所有内容/Images。添加一个?使其变得懒惰:http.*?/Images有关 MSDN 上贪婪和惰性量词的更多信息这个 Regex Storm 上的 Regex不过要小心,您的正则表达式也会匹配其中包含/Images的其他路径,例如:http://localhost:59455/Whatever/Images http://localhost:59455/ImagesButDifferent所以你可能想让它更具限制性。
打开App,查看更多内容
随时随地看视频慕课网APP