内容
Hello World.
<a href="#" target=_blank>hello World</a>
如何替换html代码中的
并将其他
保留在文本中。
答案 0 :(得分:16)
对我而言,最好的是:
Imports System.Web
HttpUtility.HtmlDecode(codeHtml)
答案 1 :(得分:4)
您可以尝试搜索
(?<=<[^>]*)
并用一个空格替换它?
这会查找
内部标记(前面有<
,可能还有>
以外的其他字符)。
<
/ >
符号,则会失败。最好避免将
放在错误的位置。
答案 2 :(得分:4)
很简单
youString.Replace(" ", " ");
String类 http://msdn.microsoft.com/en-us/library/system.string.aspx
答案 3 :(得分:1)
string A = HttpContext.Current.Server.HtmlDecode(Text);
string A = Text.Replace(" "," ");
string A = Text.Replace("&nbsp;", " ");
↑ &nbsp;
答案 4 :(得分:0)
这会找到包含&amp; nbsp:
的所有文本条<[^>]+? [^<]+?>
Fropm在这里你可以做一个简单的字符串替换空格,因为Regex会在你的文本中给你匹配的lcoation。
答案 5 :(得分:0)
只需将文字替换为以下文字后的字符串。
xyz.Text.Replace(" ", string.Empty);
答案 6 :(得分:-1)
//Funciona!!!!!!!!!!!!!!!
string a =UnHtml(text);
//----------------------------------------- ------
private static readonly Regex _tags_ = new Regex(@"<[^>]+?>", RegexOptions.Multiline | RegexOptions.Compiled);
//add characters that are should not be removed to this regex
private static readonly Regex _notOkCharacter_ = new Regex(@"[^\w;&#@.:/\\?=|%!() -]", RegexOptions.Compiled);
public static String UnHtml(String html)
{
html = HttpUtility.UrlDecode(html);
html = HttpUtility.HtmlDecode(html);
html = RemoveTag(html, "<!--", "-->");
html = RemoveTag(html, "<script", "</script>");
html = RemoveTag(html, "<style", "</style>");
//replace matches of these regexes with space
html = _tags_.Replace(html, " ");
html = _notOkCharacter_.Replace(html, " ");
html = SingleSpacedTrim(html);
return html;
}
private static String RemoveTag(String html, String startTag, String endTag)
{
Boolean bAgain;
do
{
bAgain = false;
Int32 startTagPos = html.IndexOf(startTag, 0, StringComparison.CurrentCultureIgnoreCase);
if (startTagPos < 0)
continue;
Int32 endTagPos = html.IndexOf(endTag, startTagPos + 1, StringComparison.CurrentCultureIgnoreCase);
if (endTagPos <= startTagPos)
continue;
html = html.Remove(startTagPos, endTagPos - startTagPos + endTag.Length);
bAgain = true;
} while (bAgain);
return html;
}
private static String SingleSpacedTrim(String inString)
{
StringBuilder sb = new StringBuilder();
Boolean inBlanks = false;
foreach (Char c in inString)
{
switch (c)
{
case '\r':
case '\n':
case '\t':
case ' ':
if (!inBlanks)
{
inBlanks = true;
sb.Append(' ');
}
continue;
default:
inBlanks = false;
sb.Append(c);
break;
}
}
return sb.ToString().Trim();
}
https://newbedev.com/remove-html-tags-from-string-including-nbsp-in-c