7

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

2
  • 3
    Take a look in this link : stackoverflow.com/questions/1665829/… Commented Jan 29, 2012 at 12:08
  • Is jQuery a requirement or does the link above suit your needs? Commented Jan 30, 2012 at 13:55

7 Answers 7

30

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(',')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks the first str.split(/[ ,]+/).join(',') did the work like a charm
12

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)

Comments

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

http://jsfiddle.net/yeefR/

Cheers!

Comments

1

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.

Comments

1

This is working fine without using filters

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.