How to limit spaces and breaks lines in Draft JS?

橙三吉。 提交于 2020-07-22 21:33:28

问题


I am trying to clean my html content before save it to my database. I need to limit both vertical and horizontal spaces. I don't want to allow more than 1 space in both cases.

However, the javascript replace() function seems to be inappropriate.

content = content.replace(/( ){2,}/gm," ") //horizontal spaces

content = content.replace(/(<p><br><\/p>){2,}/gm,'\v') //Vertical spaces

None of the above operations tried above seems to be working. Moreover html tags are not recognized in my regex whereas text I can replace text inside them. Yet my html content has been successfully converted into string.

How can I trim these additional spaces or at least prevent user to make multiple space input ?

Thanks .

Edit

HTML Content :

<p><em>Lorem ilolosum </em>dolor sit amet, consectetur adiloloiscing elit. Mauris tortor felis, volutpat sit amet maximus nec, tempus auctor diam. Nunc odio elit,  commodo quis dolor in, sagittis scelerisque nibh. Susloloendisse consequat, sapien sit amet pulvinar  tristique, augue ante daloloibus nulla, eget gravida turpis est sit amet nulla. Vestibulum lacinia mollis  accumsan. Vivamus loloorta cursus libero vitae mattis. In gravida bibendum orci, id faucibus felis molestie ac.  Etiam vel elit cursus, scelerisque dui quis, auctor risus.</p>

回答1:


For the horizontal space, you can use the meta sequence \s to find any space character like this:

content = content.replace(/\s{2,}/gm, ' ') //horizontal spaces

For the "vertical space" (it's juste a new line), juste remove the element:

content = content.replace(/<p><br><\/p>/gm, '') //Vertical spaces


来源:https://stackoverflow.com/questions/42993638/how-to-limit-spaces-and-breaks-lines-in-draft-js

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