Javascript 用javascript用逗号替换空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9053016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replace spaces with commas with javascript
提问by winter sun
I have a string with keywords and I need to check if this string contains spaces and if yes replace them with commas.
the string can be something like "keyword1 keyword2 ,keyword3 keyword4,keyword5"
or any other combination of spaces and commas.
the final result should be a string of keywords separated by commas without any spacing
like in the following "keyword1,keyword2,keyword3,keyword4,keyword5".
for that I tried to do $("#strId").split('').join(',')
This done the job but I notice that if I have a string which contains more then one space between each keyword I got multiple commas like that:
original string=(keyword1 keyword2 keyword3)
result string =(keyword1,,,,,,keyword2,,,,,,keyword3)
and I need that it will be with single comma only between each word.
I will appreciate a help on this issue
我有一个带关键字的字符串,我需要检查这个字符串是否包含空格,如果是,用逗号替换它们。
字符串可以是“keyword1 keyword2 ,keyword3 keyword4,keyword5”
或任何其他空格和逗号的组合。最终结果应该是一串由逗号分隔的关键字,没有任何空格,
如下面的"keyword1,keyword2,keyword3,keyword4,keyword5"。
为此,我尝试$("#strId").split('').join(',')
这样做完成了这项工作,但我注意到如果我有一个字符串,其中每个关键字之间包含多个空格,我会得到多个逗号,如下所示:
原始字符串=(关键字1关键字2关键字3 )
结果字符串=(关键字1 ,, ,,,,keyword2,,,,,,keyword3)
我需要它在每个单词之间只用一个逗号。我会很感激在这个问题上的帮助
Thanks
谢谢
回答by Gumbo
Split on any sequence of spaces and commas:
在任何空格和逗号序列上拆分:
str.split(/[ ,]+/).join(',')
You might also want to use filter
to remove empty strings:
您可能还想使用filter
删除空字符串:
str.split(/[ ,]+/).filter(function(v){return v!==''}).join(',')
Another solution would be to match any sequence that does not contain a space or comma:
另一种解决方案是匹配任何不包含空格或逗号的序列:
str.match(/[^ ,]+/g).join(',')
回答by nnnnnn
Use the String.replace()
method.
var newString = yourString.replace(/[ ,]+/g, ",");
This says to replace any sequence of one or more spaces or commas in a row with a single comma, so you're covered for strings where the commas have spaces around them like "test, test,test test test , test"
.
这表示用单个逗号替换一行中的一个或多个空格或逗号的任何序列,因此您可以覆盖逗号周围有空格的字符串,例如"test, test,test test test , test"
.
(Note: if you want to allow for other whitespace characters beyond just a space use \s
in the regular expression: /[\s,]+/g
)
(注:如果你想允许不仅仅是一个空间的使用其他空白字符\s
在正则表达式:/[\s,]+/g
)
回答by Roel
alert("test test, test".replace(/[ ,]+/g, ","))
Cheers!
干杯!
回答by Software Guy
This should work fine using regular expressions and handles any number of spaces:
使用正则表达式应该可以正常工作并处理任意数量的空格:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="strId">keyword1 keyword2 ,keyword3 keyword4,keyword5</div>
<script>
var arr = $("#strId").html().split(/\s+,|\s+/).join(',');
alert(arr);
</script>
Also, just FYI: split() is a javascript function, and works fine without jquery too.
另外,仅供参考:split() 是一个 javascript 函数,在没有 jquery 的情况下也能正常工作。
回答by user4093939
This is working fine without using filters
这在不使用过滤器的情况下工作正常
var input = $("#seperate").val();
var output = input.split(" ").join(';');
回答by Thorsten Hans
You could easily use the replace method form string. you don't have to use jQuery have a look at http://w3schools.com/jsref/jsref_replace.asp
您可以轻松地使用替换方法表单字符串。你不必使用jQuery看看http://w3schools.com/jsref/jsref_replace.asp