0

I have this string 1,2,3,4,5

and say i remove 1 then it becomes ,2,3,4,5 or 1,2,,4,5

how do I remove "1," or any number from the list and replace those extra commas and also keep in mind the last number "5" doesnt have a comma.

I can use the string replace javascript function , I am more concerned with the last number

example if i remove 5 it should show as 1,2,3,4

asked Nov 11, 2010 at 18:12
9
  • 3
    Wait - do you have a string or an array? Commented Nov 11, 2010 at 18:17
  • Looking at your comment on @webSol's answer, it sounds like you want a method to remove any given number from a comma-delimited string? If this is correct, please update your question accordingly. And clarify the whole "string or array" thing. Commented Nov 11, 2010 at 18:24
  • @jball: There is no comma-delimited string to be seen here. Consider :foo:bar:. That has 2 fields if colon-delimited, 3 fields if colon-terminated, and 4 fields if colon-separated. We're programmers: don't get sloppy. Commented Nov 11, 2010 at 18:32
  • @jball that right its a string Commented Nov 11, 2010 at 18:37

4 Answers 4

4
theString.replace(/«the number»,?|,«the number»$/, '')
>>> "1,2,3,4,5".replace(/1,?|,1$/, '')
"2,3,4,5"
>>> "1,2,3,4,5".replace(/2,?|,2$/, '')
"1,3,4,5"
>>> "1,2,3,4,5".replace(/5,?|,5$/, '')
"1,2,3,4"

Or treat the string as an array, with

theString.split(/,/).filter(function(x){return x!="«the number»";}).join(",")
>>> "1,2,3,4,5".split(/,/).filter(function(x){return x!="1";}).join(",")
"2,3,4,5"
>>> "1,2,3,4,5".split(/,/).filter(function(x){return x!="2";}).join(",")
"1,3,4,5"
>>> "1,2,3,4,5".split(/,/).filter(function(x){return x!="5";}).join(",")
"1,2,3,4"
answered Nov 11, 2010 at 18:14
Sign up to request clarification or add additional context in comments.

Comments

2

Don't use regular expression. Use arrays. You can split() your string into an array on the comma, then remove the elements as needed. You can then use join() to put them back together as a string.

answered Nov 11, 2010 at 18:15

4 Comments

+1 - though you should mention join to rebuild the string from the array
var str = '1,2,3,4,5'; str = str.replace('1,', ''); I am trying to remove any of the numbers but this fails when you try to replace "5" str = str.replace('5,', '');
@user244394 str.replace is not mentioned in this answer, and you are providing conflicting specifications in your question and subsequent comments.
I was trying couple thing split and replace
1
function removeValue(value, commaDelimitedString)
{
 var items = commaDelimitedString.split(/,/);
 var idx = items.indexOf(value);
 if(idx!=-1) { items.splice(idx, 1); }
 return items.join(",");
}
answered Nov 11, 2010 at 18:36

2 Comments

+1 nice function. I guess not as pretty as KennyTM though ;)
@Jason McCreary, Thanks, though to be fair, I voted for KennyTM's as well :)
0

you can use split and merge functions in javascript

answered Nov 11, 2010 at 18:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.