如何在 Draft JS 中限制空格和换行? [英] How to limit spaces and breaks lines in Draft JS?

查看:68
本文介绍了如何在 Draft JS 中限制空格和换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在将 html 内容保存到我的数据库之前清理它.我需要限制垂直和水平空间.在这两种情况下,我都不想允许超过 1 个空格.

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.

然而,javascript replace() 函数似乎不合适.

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

以上尝试的上述操作似乎都不起作用.此外,我的 regex 无法识别 html 标签,而文本我可以替换其中的文本.然而我的 html 内容已成功转换为字符串.

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 ?

谢谢.

编辑

HTML 内容:

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

推荐答案

对于水平空间,可以使用元序列 \s 来查找任意的空格字符,如下所示:

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

这篇关于如何在 Draft JS 中限制空格和换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆