Trimming whitespace from HTML content?

巧了我就是萌 提交于 2019-12-13 03:29:28

问题


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)


回答1:


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



来源:https://stackoverflow.com/questions/1550532/trimming-whitespace-from-html-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!