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;
}
-
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).
Mithun P : Thanks, but it is for removing white spaces not for commaa -
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 endEDIT: Made all the changes - should work now.
paxdiablo : 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.Mithun P : 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,, ";Crimson : All changes made -
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
trueforcollapsewill collapse the spaces within a section (a section being defined as the characters between commas). - Passing
trueforaddSpacewill 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; }Mithun P : 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'paxdiablo : 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".Mithun P : Great it worked fine. s = ",chrome, firefox, , ,,, , , Internet Explorer, safari, opera,, " yields "chrome,firefox,Internet Explorer,safari,opera" what i actually want - Passing
-
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.
Boldewyn : Someone voted this answer down. Would you care to explain? At first sight I see no problem with it.paxdiablo : Possibly because it doesn't seem to handle ", ," in the middle of a string (the "and if possible" bit from the question)?reko_t : Missed that, sorry. Fixed the regexp to deal with that too.Mithun P : pretty good, but does not handle spaces. s = ",chrome, firefox, , , Internet Explorer, safari, opera,, "; i got ",chrome, firefox, , , Internet Explorer, safari, opera,, "reko_t : 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"Mithun P : Little surprise s = ",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!!!paxdiablo : @Mithun, you can't just say s.replace(), you have to say s = s.replace().Mithun P : @paxdiablo Thanks for correcting me @reko_t This is perfect answer! -
My take:
var cleanStr = str.replace(/^[\s,]+/,"") .replace(/[\s,]+$/,"") .replace(/\s*,+\s*(,+\s*)*/g,",")This one will work with
opera, internet explorer, whateverActually 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?Mithun P : Not at all working!Mithun P : now it is workingVictor : Cool, thanks for the feedbackpaxdiablo : 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)? :-)Victor : 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 -
match() is much better tool for this than replace()
str = " aa, bb,, cc , dd,,,"; newStr = str.match(/[^\s,]+/g).join(",") alert("[" + newStr + "]")Mithun P : This removes all the white spaces and replaces with comma Fails for ",chrome, firefox, ,, Internet Explorer, safari, opera,, "stereofrog : feel free to improve ;)
0 comments:
Post a Comment