winter sun
winter sun

Reputation: 582

replace spaces with commas with javascript

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

Thanks

Upvotes: 7

Views: 43160

Answers (7)

Anecha Sun
Anecha Sun

Reputation: 155

try this code

var text = '           ,        x  ,x,   x,x abc,x,             x                    ,xxxxxx                  ,             ';

text = text.replace(/(\s+)?,(\s+)?/g, ',');
console.log(text);
// => ,x,x,x,x abc,x,x,xxxxxx,

var values = text.split(',').filter(value => value);
console.log(values);
// => ["x", "x", "x", "x abc", "x", "x", "xxxxxx"]

Upvotes: 0

user4093939
user4093939

Reputation: 11

This is working fine without using filters

var input = $("#seperate").val();
var output = input.split(" ").join(';');

Upvotes: 1

Software Guy
Software Guy

Reputation: 3280

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.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655707

Split on any sequence of spaces and commas:

str.split(/[ ,]+/).join(',')

You might also want to use filter to remove empty strings:

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(',')

Upvotes: 30

Roel
Roel

Reputation: 326

alert("test test, test".replace(/[ ,]+/g, ","))

http://jsfiddle.net/yeefR/

Cheers!

Upvotes: 3

nnnnnn
nnnnnn

Reputation: 150070

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".

(Note: if you want to allow for other whitespace characters beyond just a space use \s in the regular expression: /[\s,]+/g)

Upvotes: 12

Thorsten Hans
Thorsten Hans

Reputation: 2683

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

Upvotes: 0

Related Questions