Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在javascript和c中使用正则表达式删除空行#_C#_Javascript_Regex_Dom_Html Agility Pack - Fatal编程技术网

C# 如何在javascript和c中使用正则表达式删除空行#

C# 如何在javascript和c中使用正则表达式删除空行#,c#,javascript,regex,dom,html-agility-pack,C#,Javascript,Regex,Dom,Html Agility Pack,用户通过文本编辑器输入内容,最后提交到数据库。 在存储到数据库之前,我希望删除内容开头和结尾的空行(中间不能删除) 我想使用JavaScript和C# 样本内容为: <div> <p><span><br></span></p> <span>a<br/>bc</span> <p>te<br>st</p> <p>\

用户通过文本编辑器输入内容,最后提交到数据库。 在存储到数据库之前,我希望删除内容开头和结尾的空行(中间不能删除)

我想使用JavaScript和C#

样本内容为:

<div>
    <p><span><br></span></p>
    <span>a<br/>bc</span>
    <p>te<br>st</p>
    <p>\n<span>\n</span></p>
    <p><span><br/></span></p>
</div>


a
bc te
st

\n\n


我需要的是:

<div>
    <span>a<br/>bc</span>
    <p>te<br>st</p>
</div>

a
bc te
st


谁能帮我?

好吧,如果我了解你想要实现的目标,这应该能解决你的问题:

        string input = @"
        <div>
            <p><span><br></span></p>
            <span>a<br/>bc</span>
            <p>te<br>st</p>
            <p>\n<span>\n</span></p>
            <p><span><br/></span></p>
        </div>
        ";
        string pattern = @"(<p>)?(\\n|<br/?>)?<span>(<br/?>|\\n)</span>(</p>)?";
        System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern);
        string final = reg.Replace(input, String.Empty);
        Console.WriteLine(final);
    }
字符串输入=@”

a
bc te
st

\n\n


"; 字符串模式=@“()?(\\n|)?(\124;\\ n)(

)?”; System.Text.RegularExpressions.Regex reg=新的System.Text.RegularExpressions.Regex(模式); 字符串final=reg.Replace(输入,字符串为空); 控制台写入线(最终); }
上述代码将返回:

<div>

                <span>a<br/>bc</span>
                <p>te<br>st</p>


</div>

a
bc te
st


然后,您可以开始修剪任何一行,因为它看起来需要它。

问题中没有提到您是要清理客户端还是服务器端的内容

如果应该在服务器上完成,请不要使用正则表达式。为什么?请看精彩的答案。改用HTML解析器。例如,使用HtmlAgiltyPack:

var doc = new HtmlDocument();
doc.LoadHtml(html);
foreach(var node in doc.DocumentNode.SelectNodes("//div|//span|//p"))
    if (string.IsNullOrWhiteSpace(node.InnerText.Replace(@"\n", string.Empty)))
        node.Remove();

var result = doc.DocumentNode.OuterHtml;
但通过使用jQuery,在客户机上(也没有regex)可以做得更简单:

var dom = $(html);
dom.find('p,span,div').each(function() {
    if ($(this).text().trim() == '')
        $(this).remove();
});

var result = dom.wrap('<div>').parent().html();
vardom=$(html);
find('p,span,div')。每个(函数(){
如果($(this).text().trim()='')
$(this.remove();
});
var result=dom.wrap(“”).parent().html();

标记是否总是在
标记之外?你甚至可以依靠用户输入标签来平衡吗?在您的“示例内容”中,带
\n
的行是在一行(文本)上,还是在输入字符串中实际有换行符?@mathematic.coffee\n和
(或
)由文本编辑器创建。您想使用JavaScript还是C?使用C#最简单的方法是使用HTML解析器并遍历生成的树以查找相邻的空节点。ReGEX在这里帮不上忙。我们只需要添加这个链接:考虑使用DOM树并递归地遍历所有节点,删除那些只包含空白的代码(C*<代码>字符串。IsNullOrWhiteSpace(No.InnerText)),然后你就完成了。浏览器中的Javascript DOM或C#中的HTML Agility Pack都可以让您这样做。