views:

660

answers:

6

I want to remove all unnecessary commas from the start/end of the string.

eg; google, yahoo,, , should become google, yahoo.

If possible ,google,, , yahoo,, , should become google,yahoo.

I've tried the below code as a starting point, but it seems to be not working as desired.

trimCommas = function(s) {
 s = s.replace(/,*$/, "");
 s = s.replace(/^\,*/, "");
 return s;
}
+1  A: 

First ping on Google for "Javascript Trim": http://www.somacon.com/p355.php. You seem to have implemented this using commas, and I don't see why it would be a problem (though you escaped in the second one and not in the first).

Kevin Peno
Thanks, but it is for removing white spaces not for commaa
Mithun P
+3  A: 

You need this:

s = s.replace(/[,\s]{2,}/,""); //Removes double or more commas / spaces
s = s.replace(/^,*/,""); //Removes all commas from the beginning
s = s.replace(/,*$/,""); //Removes all commas from the end

EDIT: Made all the changes - should work now.

Crimson
Almost, but not quite. Fixed that for ya' :-)
paxdiablo
Actually, this still has problems I didn't detect - it doesn't handle the spaces. You may want to fix that.
paxdiablo
This is fine, excepts it does not handle spaces.for s = ",chrome, firefox, ,, Internet Explorer, safari, opera,, ";i got "chrome, firefox, Internet Explorer, safari, opera,, "
Mithun P
Fails for s = ",chrome, firefox, ,, , Internet Explorer, safari, opera,, ";
Mithun P
All changes made
Crimson
+1  A: 

What you need to do is replace all groups of "space and comma" with a single comma and then remove commas from the start and end:

trimCommas = function(s) {
    s = s.replace(/[,\s]*,[,\s]*/g, ",");
    s = s.replace(/^,/, "");
    s = s.replace(/,$/, "");
    return s;
}

The first one replaces every sequence of whitespace and commas with a single comma, provided there's at least one comma in there. This handles the edge case left in the comments for "Internet Explorer".

The second and third get rid of the comma at the start and end of string where necessary.

You can also add (to the end):

s = s.replace(/[\s]+/, " ");

to collapse multi-spaces down to one space and

s = s.replace(/,/g, ", ");

if you want them to be formatted nicely (space after each comma).

A more generalized solution would be to pass parameters to indicate behavior:

  • Passing true for collapse will collapse the spaces within a section (a section being defined as the characters between commas).
  • Passing true for addSpace will use ", " to separate sections rather than just "," on its own.

That code follows. It may not be necessary for your particular case but it might be better for others in terms of code re-use.

trimCommas = function(str,collapse,addspace) {
    s = s.replace(/[,\s]*,[,\s]*/g, ",").replace(/^,/, "").replace(/,$/, "");
    if (collapse) {
        s = s.replace(/[\s]+/, " ");
    }
    if (addspace) {
        s = s.replace(/,/g, ", ");
    }
    return s;
}
paxdiablo
not, for s = ",chrome, firefox, ,, Internet Explorer, safari, opera,, ";i got "chrome,firefox,Internet,Explorer,safari,opera"i eed to keep the space between 'internet' and 'explorer'
Mithun P
See the updated first replace. It now replaces any sequence of spaces and commas with a comma, *provided* there's at least one comma in there. That should handle "Internet Explorer".
paxdiablo
Great it worked fine.s = ",chrome, firefox, , ,,, , , Internet Explorer, safari, opera,, " yields "chrome,firefox,Internet Explorer,safari,opera" what i actually want
Mithun P
+1  A: 

In your example you also want to trim the commas if there's spaces between them at the start or at the end, use something like this:

str.replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/g, ',');

Note the use of the 'g' modifier for global replace.

reko_t
Someone voted this answer down. Would you care to explain? At first sight I see no problem with it.
Boldewyn
Possibly because it doesn't seem to handle ", ," in the middle of a string (the "and if possible" bit from the question)?
paxdiablo
Missed that, sorry. Fixed the regexp to deal with that too.
reko_t
pretty good, but does not handle spaces.s = ",chrome, firefox, , , Internet Explorer, safari, opera,, ";i got ",chrome, firefox, , , Internet Explorer, safari, opera,, "
Mithun P
It does handle spaces, that's what the \s is for (whitespaces, including tabs and newlines):>>> ",chrome, firefox, , , Internet Explorer, safari, opera,, ".replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/, ',')"chrome, firefox, Internet Explorer, safari, opera"
reko_t
Little surprises = ",chrome, firefox, , , Internet Explorer, safari, opera,, ";s.replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/, ',')alert(s);is not working!!But ",chrome, firefox, , , Internet Explorer, safari, opera,, ".replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/, ',') is working!!!
Mithun P
@Mithun, you can't just say s.replace(), you have to say s = s.replace().
paxdiablo
@paxdiablo Thanks for correcting me@reko_t This is perfect answer!
Mithun P
+1  A: 

My take:

var cleanStr = str.replace(/^[\s,]+/,"")
                  .replace(/[\s,]+$/,"")
                  .replace(/\s*,+\s*(,+\s*)*/g,",")

This one will work with opera, internet explorer, whatever

Actually tested this last one, and it works!

Victor
Why the downvote? What is so wrong with my regexes?
Victor
Well, they were a bit ugly, is that better?
Victor
Not at all working!
Mithun P
now it is working
Mithun P
Cool, thanks for the feedback
Victor
I think you may be trying to be a bit clever in that last one - what does it do with "a, , , , ,b" (there are spaces between all those commas)? :-)
paxdiablo
Of course I try to be clever, that's what regex are for! XD But, you are right: Let's try again. And also it would not catch "a , b"! What a disaster
Victor
A: 

match() is much better tool for this than replace()

 str  = "    aa,   bb,,   cc  , dd,,,";
 newStr = str.match(/[^\s,]+/g).join(",")
 alert("[" + newStr + "]")
stereofrog
This removes all the white spaces and replaces with commaFails for ",chrome, firefox, ,, Internet Explorer, safari, opera,, "
Mithun P
feel free to improve ;)
stereofrog