javascript - preg_replace( '/\h+/', ' ', $foo ) 在 JavaScript 中?
问题描述
如何删除多余的水平空格(空格和制表符)但保留 javascript 中的换行符?
使用 PHP:preg_replace( '/\h+/', ' ', $foo )
A lot of text text text text
A lot more text text text text text
应该看起来像:
A lot of text text text text
A lot more text text text text text
解决方案
您可以使用同时包含制表符和空格的字符集:
const str = `A lot of text text text text
And more text text text text text`;
console.log(
str.replace(/[ ]+/g, ' ')
);
或者,用\t
元字符代替:
const str = `A lot of text text text text
And more text text text text text`;
console.log(
str.replace(/[ \t]+/g, ' ')
);
推荐阅读
- file - 如何在vue js中将文件作为字节码传递给服务器端?
- javascript - 如何用数据对表格进行排序
- android-fragments - YouTubePlayerSupportFragment:一段时间后出现“播放时出现问题”错误
- c++ - 使用 STL 的部分模板特化
- spring-boot - 使用 docker 时,如何在 Eureka Client 中保持 Eureka 服务器 url 动态?
- r - R - 通过缺少值的 Dataframe 逐列循环 lm()
- altair - 如何为 mark_rect 显式设置 y 范围(热图)
- python - MySQL python 连接器更新错误:检查要在 %s 附近使用的正确语法。出了什么问题?
- css - 具有多个背景 css 背景属性的后备颜色
- ruby - 使用 watir 加载 chrome 配置文件使用错误的目录