0

I have a string with comma separated values:

var x = '1,2,10,11,12';

I need to remove the value that's in y.

x = remove(x,y);

Q: Is there a function for that already or do I need to convert it to an array, remove y and then convert it back to a string?

asked May 17, 2011 at 16:59
0

5 Answers 5

2

You can do x.replace(y, ""). For more information, see the Mozilla docs.

answered May 17, 2011 at 17:01
Sign up to request clarification or add additional context in comments.

5 Comments

just that won't be enough but it's an interesting starting point
Maybe I don't understand the problem. Are you matching strings, or numeric values in y?
Just keep in mind that using a string for the first parameter will only replace the value once. Doing String.replace(/value/g, ""); will replace all instances of it.
I think if I put a leading comma and a trailing comma in x, then I can do: x.replace(',' + y + ',',",")
@cf_PhillipSenn, that won't work when y is at the beginning or end of the list. Without the commas won't work because you might remove numbers you don't want. If y is '7', for example, you could turn '17' into '1'.
1

Do you mean like this?

var arr = "1,2,1,2,3".split(",");
var arr2 = [];
for(var i = 0; i < arr.length; i++) {
 if(arr[i] != "1") arr2.push(arr[i])
}
arr2.join(","); // "2,2,3"
answered May 17, 2011 at 17:07

4 Comments

This is grossly ugly code, I can't imagine why you would ever use this as a solution to OP question.
Why would you declare an anonymous string and then split it? why would you use a second array to copy every single negative match, rather then just removing the positive ones?
The OP has a string which needs certain elements removed, at least that's what I thought. I agree it's not the most elegant solution though.
Elegant no, but efficiency is the true problem here.
1

Assuming your removing 'y' where y is an index, do this:

x = x.split(',').splice(y,1).join(',');

Edit:

In that case, I would use regex. If you wish to avoid regex, another solution is available:

while(x.indexOf(y) >= 0){
 x.replace(y+',', '');
}

EDIT: Added a trailing comma to the replace, such that the list remains a comma delimited list.

answered May 17, 2011 at 17:06

1 Comment

y is not an index. y is a value. In this example, it would be customer ids or product ids.
0

str_replace analogon from phpjs.org: http://phpjs.org/functions/str_replace:527

So you could use something like x=str_replace(y, '', x); ;) Or implement it yourself by using regexp!

Edit: Okay I think I got it wrong. Probably Greg's answer is the right one :-)

answered May 17, 2011 at 17:06

2 Comments

PHP.js intrigues, confuses, and disgusts me, much like a 12 year old arguing religion in a chat room, or a cockroach slowly making its way through the middle of my pudding.
-1 For recommending PHPjs. You don't need that. It's only use case is strict deadlines and talented PHP developers (who know no javascript).
0

There are three things to check for

  1. y at the beginning of the string and followed by a comma
  2. y preceeded by a word break and followed by a comma
  3. y at the end of the string and preceeded by a comma

    x.replace(new RegExp('((^|\\b)' + y + ',|,' + y +'$)'),'');
    
answered May 17, 2011 at 18:53

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.