删除Javascript中的所有多个空格并替换为单个空格 [英] Remove all multiple spaces in Javascript and replace with single space

查看:110
本文介绍了删除Javascript中的所有多个空格并替换为单个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Javascript中使用单个空格自动替换多个空格的所有实例?

How can I automatically replace all instances of multiple spaces, with a single space, in Javascript?

我试过链接一些 s .replace 但这似乎不是最佳的。

I've tried chaining some s.replace but this doesn't seem optimal.

我也在使用jQuery,以防它是内置功能。<​​/ p >

I'm using jQuery as well, in case it's a builtin functionality.

推荐答案

您可以使用正则表达式替换:

You could use a regular expression replace:

str = str.replace(/ +(?= )/g,'');

信用:上述正则表达式取自正则表达式用单个空格替换多个空格

Credit: The above regex was taken from Regex to replace multiple spaces with a single space

这篇关于删除Javascript中的所有多个空格并替换为单个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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