简体   繁体   中英

Trimming whitespace from HTML content?

I have a CRUD maintenance screen with a custom rich text editor control (FCKEditor actually) and the program extracts the formatted text as HTML from the control for saving to the database. However, part of our standards is that leading and trailing whitespace needs to be stripped from the content before saving, so I have to remove extraneous &nbsp; and <br> and such from the beginning and end of the HTML string.

I can opt to either do it on the client side (using Javascript) or on the server side (using Java) Is there an easy way to do this, using regular expressions or something? I'm not sure how complex it needs to be, I need to be able to remove stuff like:

<p><br /> &nbsp;</p>

yet retain it if there's any kind of meaningful text in between. (Above snippet is from actual HTML data saved by the tester)

/<p>(?:<br\s*\/>|&[#\w]{2,6};|[\s\n\r])*?<\/p>/g

That should match all paragraphs that don't contain any "meaningful text".

It's probably best to do it on the server-side though.

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