如何更换空间?

时间:2010-09-14 12:13:31

标签: c# regex

内容

    Hello World.

<a&nbsp;href="#"&nbsp;target=_blank>hello&nbsp;World</a>

如何替换html代码中的&nbsp;并将其他&nbsp;保留在文本中。

7 个答案:

答案 0 :(得分:16)

对我而言,最好的是:

Imports System.Web
HttpUtility.HtmlDecode(codeHtml)

答案 1 :(得分:4)

您可以尝试搜索

(?<=<[^>]*)&nbsp;

并用一个空格替换它?

这会查找&nbsp;内部标记(前面有<,可能还有>以外的其他字符)。

但是,这非常脆弱。例如,如果字符串/属性中包含< / >符号,则会失败。最好避免将&nbsp;放在错误的位置。

答案 2 :(得分:4)

答案 3 :(得分:1)

string A = HttpContext.Current.Server.HtmlDecode(Text);

string A = Text.Replace("&nbsp"," ");

string A = Text.Replace("&amp;nbsp;", " ");

                           ↑ &amp;nbsp;

答案 4 :(得分:0)

这会找到包含&amp; nbsp:

的所有文本条
<[^>]+?&nbsp;[^<]+?>

Fropm在这里你可以做一个简单的字符串替换空格,因为Regex会在你的文本中给你匹配的lcoation。

答案 5 :(得分:0)

只需将文字替换为以下文字后的字符串。

  

xyz.Text.Replace("&nbsp;", 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