从字符串中删除HTML

本文关键字:HTML 删除 字符串 | 更新日期: 2023-09-27 18:12:17

我有一个表(Wijmo Grid)。列Log需要一些文本

允许用户在文本中写入HTML,因为在邮寄时也使用相同的文本以使其看起来美观且样式良好。

假设文本是:

var text = "Hello friend <br> How are you? <h1> from me </h1>";

是否有任何方法或JSON.stringify() og HTML.enocde()我可以/应该使用:

var textWithoutHtml = magic(text); // "Hello friend How are you? from me"

其中一个问题是,如果文本包含"<br>",它将切换到表的下一行,并且有可能看到该行第二行的上半部分,这看起来不太好。

从字符串中删除HTML

var text = "Hello friend <br> How are you? <h1> from me </h1>";
var newText = text.replace(/(<([^>]+)>)/ig, "");

小提琴:http://jsfiddle.net/EfRs6/

就我理解你的问题而言,你可以在c#中像这样编码值

string encodedValue= HttpUtility.HtmlEncode(txtInput.Text);

注意:这里的txtInput是你页面上的TextBox的id

您可以这样尝试:

string s = Regex.Replace("Hello friend <br> How are you? <h1> from me </h1>", @"<[^>]+>|&nbsp;", "").Trim();

你也可以查看HTML Agility Pack

这是一个敏捷的HTML解析器,它构建一个读/写DOM并支持纯XPATH或XSLT(实际上也不需要理解XPATH)XSLT来使用它,不用担心…)。它是一个。net代码库,允许解析"来自web"的HTML文件。解析器是非常宽容的使用"真实世界"的畸形HTML。对象模型非常类似于什么建议System.Xml,但用于HTML文档(或流)。

<[^>]+>|&nbsp;/
1st Alternative: <[^>]+>
< matches the characters < literally
[^>]+ match a single character not present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
> a single character in the list > literally (case sensitive)
> matches the characters > literally
2nd Alternative: &nbsp;
&nbsp; matches the characters &nbsp; literally (case sensitive)