Html 匹配img标记的Regex模式
使用MVC,看看这个示例,其中有以下HTML代码:Html 匹配img标记的Regex模式,html,asp.net-mvc,regex,model-view-controller,asp.net-mvc-4,Html,Asp.net Mvc,Regex,Model View Controller,Asp.net Mvc 4,使用MVC,看看这个示例,其中有以下HTML代码: <p>Duma</p><img url='..' /><p>Duma</p> 删除图像(显示[image])标记,仅显示文本(作为innerText) 我决定创建一个扩展,直到现在,只显示 Duma Duma public static class StringExtensions { public static string StripHtml (this string
<p>Duma</p><img url='..' /><p>Duma</p>
删除图像(显示[image])标记,仅显示文本(作为innerText)
我决定创建一个扩展,直到现在,只显示
Duma Duma
public static class StringExtensions
{
public static string StripHtml (this string inputString)
{
return Regex.Replace
(inputString, "<.*?>", string.Empty);
}
}
杜马杜马
公共静态类扩展
{
公共静态字符串StripHtml(此字符串输入字符串)
{
返回正则表达式。替换
(inputString,“,string.Empty);
}
}
每次正则表达式与img标记匹配时,如何添加[IMAGE]您可以尝试以下方法:
public static class StringExtensions
{
public static string StripHtml (this string inputString)
{
return Regex.Replace(
Regex.Replace(inputString, "<img.*?>", "[IMAGE]"), "<.*?>", string.Empty);
}
}
公共静态类StringExtensions
{
公共静态字符串StripHtml(此字符串输入字符串)
{
返回正则表达式。替换(
Regex.Replace(inputString,“[IMAGE]”,“”,string.Empty);
}
}
它实际上调用了两个rege.Replace(),一个在另一个内部!st one将
标记替换为[IMAGE],然后替换字符串的其余部分,只保留
标记内的内容。可能重复的
public static class StringExtensions
{
public static string StripHtml (this string inputString)
{
return Regex.Replace(
Regex.Replace(inputString, "<img.*?>", "[IMAGE]"), "<.*?>", string.Empty);
}
}