简体   繁体   English

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

[英]how to remove empty line use regex in javascript and c#

user input content by text editor, and finally submitted to the database. 用户通过文本编辑器输入内容,最后提交到数据库。 before store in database,i want remove empty line in content at begin and end (the middle can not be removed). 在存储到数据库之前,我想在开始和结束时删除内容中的空行(中间不能删除)。

i want use JavaScript and C# 我想使用JavaScript和C#

sample content is: 样本内容为:

<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>

i need is: 我需要的是:

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

who can help me? 谁能帮我?

Well if I understand what you are trying to accomplish, this should solve your problem: 好吧,如果我了解您要完成的工作,那么应该可以解决您的问题:

        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);
    }

That above code will return: 上面的代码将返回:

<div>

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


</div>

You could then go about trimming ever line, as it looks like it needs it. 然后,您可以根据需要修剪任何线条。

It is not mentioned in the question whether you want to clean up your content on the client or server side. 在问题中没有提到要在客户端还是在服务器端清除内容。

If it should be done on the server please don't use regex for it. 如果应该在服务器上完成,请不要使用正则表达式。 Why? 为什么? See this excellent answer. 看到这个很好的答案。 Use HTML parser instead. 请改用HTML解析器。 Eg with HtmlAgiltyPack: 例如,使用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;

But it could be done even simplier on the client (without regex too) by using jQuery: 但这可以通过使用jQuery在客户端上更简单地完成(也无需使用正则表达式):

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

var result = dom.wrap('<div>').parent().html();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM