如何使用JavaScript和正则表达式删除空格。 [英] How do I remove the space using JavaScript and Regular Expression.
问题描述
在JavaScript编码中,我得到的值如2365 2365.
我想删除数字之间的空格,应该是23652365.
我该怎么做?
请帮助。
谢谢
Harshal Raut
In JavaScript coding I am getting the value like 2365 2365.
I would like to remove the space between numbers, it should be 23652365.
How should I do this?
Kindly help.
Thanks
Harshal Raut
推荐答案
试试这个:
Try this:
var withSpaces = prompt('Enter a number please:');
var withoutSpaces = withSpaces.replace(/\s/g, '');
之后
表示 g
> / \s / 全局匹配
,这意味着它将删除所有空白。如果你删除 g
那么它只会删除第一个空格/制表符/换行符。
在线演示: http://jsfiddle.net/ProgramFOX/RwF9Y/ [ ^ ]
The g
after /\s/
means global match
, that means that it will remove all whitespace. If you remove the g
then it will only remove the first space/tab/newline.
Online demo: http://jsfiddle.net/ProgramFOX/RwF9Y/[^]
您好,
最好分享你的代码,但是以通用方式这是你的查询的解决方案
Hello ,
It is better to share your code but in a generic way this is the solution for your query
val = val.replace(/\s/g, '')
其中g代表一般
最好的:)如果你回信请写回来需要任何支持。
Where g stands for general
All the best:)Pls write back if you need any support.
尝试
Try
var str = "2365 2365"
str = str.replace(/\s+/g, '');
这篇关于如何使用JavaScript和正则表达式删除空格。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!