我是正则表达式的新手,尝试过滤 HTML 标签,仅保留必需的 (src / href / style) 属性及其值,并删除不必要的属性。在谷歌搜索时,我发现一个正则表达式只保留“src”属性,因此我修改后的表达式如下:
<([a-z][a-z0-9]*)(?:[^>]*(\s(src|href|style)=['\"][^'\"]*['\"]))?[^>]*?(\/?)>
Run Code Online (Sandbox Code Playgroud)
它工作正常,但唯一的问题是,如果一个标签包含多个必需属性,那么它只保留最后一个匹配的单个属性并丢弃其余的属性。
我正在尝试清理以下文本
<title>Hello World</title>
<div fadeout"="" style="margin:0px;" class="xyz">
<img src="abc.jpg" alt="" />
<p style="margin-bottom:10px;">
The event is celebrating its 50th anniversary Kö
<a style="margin:0px;" href="http://www.germany.travel/">exhibition grounds in Cologne</a>.
</p>
<p style="padding:0px;"></p>
<p style="color:black;">
<strong>A festival for art lovers</strong>
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
在https://regex101.com/#javascript使用上述表达式作为<$1$2$4>
替换字符串并获得以下输出:
<title>Hello World</title>
<div style="margin:0px;">
<img src="abc.jpg"/>
<p style="margin-bottom:10px;">
The event is celebrating its 50th anniversary Kö
<a href="http://www.germany.travel/">exhibition grounds in Cologne</a>.
</p>
<p style="padding:0px;"></p>
<p …
Run Code Online (Sandbox Code Playgroud) 我有一个ASP.Net Core 2.0 webapi代码,如下所示
public class TestController : Controller
{
private readonly ISampleRepository _sampleRepository = new SampleRepository();
[HttpPost("{id}")]
public async void Post([FromRoute]int id, IEnumerable<IFormFile> files, [FromForm] NotifyViewModel model)
{
// get data from DB using async
var dbData = await _sampleRepository.GetAsync(id);
//check if email alert is enabled
if(dbData.IsEmailEnabled)
{
//create MailMessage
var mailMessage = new MailMessage() {
//options here
};
foreach (var formFile in files)
{
mailMessage.Attachments.Add(new Attachment(formFile.OpenReadStream(), formFile.FileName, formFile.ContentType));
}
// send email
_emailHelper.SendEmail(mailMessage);
}
}
}
public class …
Run Code Online (Sandbox Code Playgroud)