I have strings with extra whitespaces, each time there's more than only one whitespace I'd like it be only one.
我有带有额外空格的字符串,每次都有不止一个空格,我希望它只有一个空格。
Anyone? I tried searching google, but nothing worked for me.
有人知道吗?我试着搜索谷歌,但什么都没用。
Thanks
谢谢
11 个解决方案
#1
340
Something like this:
是这样的:
s.replace(/\s+/g, ' ');
#2
61
You can augment String to implement these behaviors as methods, as in:
你可以增加字符串来实现这些行为作为方法,如:
String.prototype.killWhiteSpace = function() {
return this.replace(/\s/g, '');
};
String.prototype.reduceWhiteSpace = function() {
return this.replace(/\s+/g, ' ');
};
This now enables you to use the following elegant forms to produce the strings you want:
现在,您可以使用以下优雅的表单来生成所需的字符串:
"Get rid of my whitespaces.".killWhiteSpace();
"Get rid of my extra whitespaces".reduceWhiteSpace();
#3
20
using a regular expression with the replace function does the trick:
使用替换函数的正则表达式可以实现以下目的:
string.replace(/\s/g, "")
#4
12
I presume you're looking to strip spaces from the beginning and/or end of the string (rather than removing all spaces?
我猜您是想从字符串的开头和/或结尾删除空格(而不是删除所有空格?
If that's the case, you'll need a regex like this:
如果是这样的话,您需要这样的regex:
mystring = mystring.replace(/(^\s+|\s+$)/g,' ');
This will remove all spaces from the beginning or end of the string. If you only want to trim spaces from the end, then the regex would look like this instead:
这将删除字符串开头或结尾的所有空格。如果您只想要从末尾处修剪空格,那么regex将如下所示:
mystring = mystring.replace(/\s+$/g,' ');
Hope that helps.
希望有帮助。
#5
11
Here's a non-regex solution (just for fun):
var s = ' a b word word. word, wordword word ';
// with ES5:
s = s.split(' ').filter(function(n){ return n != '' }).join(' ');
console.log(s); // "a b word word. word, wordword word"
// or ES6:
s = s.split(' ').filter(n => n).join(' ');
console.log(s); // "a b word word. word, wordword word"
It splits the string by it's whitespaces, remove them all from the array, and joins all the words again, with a single whitespace in between them.
它通过它的空格来分割字符串,将它们从数组中删除,然后再次连接所有的单词,其中只有一个空格。
#6
9
jQuery.trim() works well.
jQuery.trim()是有效的。
http://api.jquery.com/jQuery.trim/
http://api.jquery.com/jQuery.trim/
#7
7
I know I should not necromancy on a subject, but given the details of the question, I usually expand it to mean:
我知道我不应该在某一主题上使用巫术,但考虑到问题的细节,我通常会将其扩展为:
- I want to replace multiple occurences of whitespace inside the string with a single space
- 我想用一个空格替换字符串中出现的多个空格
- ...and... I do not want whitespaces in the beginnin or end of the string (trim)
- …和…我不希望在字符串的开头或结尾使用空格(修饰)
For this, I use code like this (the parenthesis on the first regexp are there just in order to make the code a bit more readable ... regexps can be a pain unless you are familiar with them):
为此,我使用这样的代码(第一个regexp上的括号只是为了让代码更可读一些……regexp可能是一种痛苦,除非您熟悉它们):
s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');
The reason this works is that the methods on String-object return a string object on which you can invoke another method (just like jQuery & some other libraries). Much more compact way to code if you want to execute multiple methods on a single object in succession.
这样做的原因是字符串对象上的方法返回一个字符串对象,您可以在该对象上调用另一个方法(就像jQuery和其他一些库一样)。如果您想要在一个对象上连续执行多个方法,那么可以使用更紧凑的编码方式。
#8
5
var x = " Test Test Test ".split(" ").join(""); alert(x);
var x =“测试测试”。分割(" "). join(" ");警报(x);
#9
2
How about this one?
这一个怎么样?
"my test string \t\t with crazy stuff is cool ".replace(/\s{2,9999}|\t/g, ' ')
“我的测试字符串\t\t与疯狂的东西是很酷的”。替换(/ \ s { 9999 } | \ t / g,' ')
outputs "my test string with crazy stuff is cool "
输出"my test string with crazy stuff is cool "
This one gets rid of any tabs as well
这个也可以去掉任何标签。
#10
1
If you want to restrict user to give blank space in the name just create a if statement and give the condition. like I did:
如果要限制用户在名称中留出空格,只需创建If语句并给出条件。像我一样:
$j('#fragment_key').bind({
keypress: function(e){
var key = e.keyCode;
var character = String.fromCharCode(key);
if(character.match( /[' ']/)) {
alert("Blank space is not allowed in the Name");
return false;
}
}
});
- create a JQuery function .
- 创建JQuery函数。
- this is key press event.
- 这是关键的新闻事件。
- Initialize a variable.
- 初始化一个变量。
- Give condition to match the character
- 给出与字符匹配的条件
- show a alert message for your matched condition.
- 为匹配的条件显示一个警告消息。
#11
0
Try this.
试试这个。
var string = " string 1";
string = string.trim().replace(/\s+/g, ' ');
the result will be
结果将是
string 1
What happened here is that it will trim the outside spaces first using trim()
then trim the inside spaces using .replace(/\s+/g, ' ')
.
这里发生的情况是,首先使用trim()修剪外部空间,然后使用.replace(/\s+/g, ')修剪内部空间。
#1
340
Something like this:
是这样的:
s.replace(/\s+/g, ' ');
#2
61
You can augment String to implement these behaviors as methods, as in:
你可以增加字符串来实现这些行为作为方法,如:
String.prototype.killWhiteSpace = function() {
return this.replace(/\s/g, '');
};
String.prototype.reduceWhiteSpace = function() {
return this.replace(/\s+/g, ' ');
};
This now enables you to use the following elegant forms to produce the strings you want:
现在,您可以使用以下优雅的表单来生成所需的字符串:
"Get rid of my whitespaces.".killWhiteSpace();
"Get rid of my extra whitespaces".reduceWhiteSpace();
#3
20
using a regular expression with the replace function does the trick:
使用替换函数的正则表达式可以实现以下目的:
string.replace(/\s/g, "")
#4
12
I presume you're looking to strip spaces from the beginning and/or end of the string (rather than removing all spaces?
我猜您是想从字符串的开头和/或结尾删除空格(而不是删除所有空格?
If that's the case, you'll need a regex like this:
如果是这样的话,您需要这样的regex:
mystring = mystring.replace(/(^\s+|\s+$)/g,' ');
This will remove all spaces from the beginning or end of the string. If you only want to trim spaces from the end, then the regex would look like this instead:
这将删除字符串开头或结尾的所有空格。如果您只想要从末尾处修剪空格,那么regex将如下所示:
mystring = mystring.replace(/\s+$/g,' ');
Hope that helps.
希望有帮助。
#5
11
Here's a non-regex solution (just for fun):
var s = ' a b word word. word, wordword word ';
// with ES5:
s = s.split(' ').filter(function(n){ return n != '' }).join(' ');
console.log(s); // "a b word word. word, wordword word"
// or ES6:
s = s.split(' ').filter(n => n).join(' ');
console.log(s); // "a b word word. word, wordword word"
It splits the string by it's whitespaces, remove them all from the array, and joins all the words again, with a single whitespace in between them.
它通过它的空格来分割字符串,将它们从数组中删除,然后再次连接所有的单词,其中只有一个空格。
#6
9
jQuery.trim() works well.
jQuery.trim()是有效的。
http://api.jquery.com/jQuery.trim/
http://api.jquery.com/jQuery.trim/
#7
7
I know I should not necromancy on a subject, but given the details of the question, I usually expand it to mean:
我知道我不应该在某一主题上使用巫术,但考虑到问题的细节,我通常会将其扩展为:
- I want to replace multiple occurences of whitespace inside the string with a single space
- 我想用一个空格替换字符串中出现的多个空格
- ...and... I do not want whitespaces in the beginnin or end of the string (trim)
- …和…我不希望在字符串的开头或结尾使用空格(修饰)
For this, I use code like this (the parenthesis on the first regexp are there just in order to make the code a bit more readable ... regexps can be a pain unless you are familiar with them):
为此,我使用这样的代码(第一个regexp上的括号只是为了让代码更可读一些……regexp可能是一种痛苦,除非您熟悉它们):
s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');
The reason this works is that the methods on String-object return a string object on which you can invoke another method (just like jQuery & some other libraries). Much more compact way to code if you want to execute multiple methods on a single object in succession.
这样做的原因是字符串对象上的方法返回一个字符串对象,您可以在该对象上调用另一个方法(就像jQuery和其他一些库一样)。如果您想要在一个对象上连续执行多个方法,那么可以使用更紧凑的编码方式。
#8
5
var x = " Test Test Test ".split(" ").join(""); alert(x);
var x =“测试测试”。分割(" "). join(" ");警报(x);
#9
2
How about this one?
这一个怎么样?
"my test string \t\t with crazy stuff is cool ".replace(/\s{2,9999}|\t/g, ' ')
“我的测试字符串\t\t与疯狂的东西是很酷的”。替换(/ \ s { 9999 } | \ t / g,' ')
outputs "my test string with crazy stuff is cool "
输出"my test string with crazy stuff is cool "
This one gets rid of any tabs as well
这个也可以去掉任何标签。
#10
1
If you want to restrict user to give blank space in the name just create a if statement and give the condition. like I did:
如果要限制用户在名称中留出空格,只需创建If语句并给出条件。像我一样:
$j('#fragment_key').bind({
keypress: function(e){
var key = e.keyCode;
var character = String.fromCharCode(key);
if(character.match( /[' ']/)) {
alert("Blank space is not allowed in the Name");
return false;
}
}
});
- create a JQuery function .
- 创建JQuery函数。
- this is key press event.
- 这是关键的新闻事件。
- Initialize a variable.
- 初始化一个变量。
- Give condition to match the character
- 给出与字符匹配的条件
- show a alert message for your matched condition.
- 为匹配的条件显示一个警告消息。
#11
0
Try this.
试试这个。
var string = " string 1";
string = string.trim().replace(/\s+/g, ' ');
the result will be
结果将是
string 1
What happened here is that it will trim the outside spaces first using trim()
then trim the inside spaces using .replace(/\s+/g, ' ')
.
这里发生的情况是,首先使用trim()修剪外部空间,然后使用.replace(/\s+/g, ')修剪内部空间。