简体   繁体   中英

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#

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. Eg with 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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