1

I'm trying to return a comma separated string without the items that end in 'non'.

Source:

id = '2345,45678,3333non,489,2333non';  

Expected Result:

id = '2345,45678,489'; 

I'm using code that I found here: remove value from comma separated values string

var removeValue = function(list, value, separator) {
  separator = separator || ",";
  var values = list.split(separator);
  for (var i = 0; i < values.length; i++) {
    if (values[i] == value) {
      values.splice(i, 1);
      return values.join(separator);
    }
  }
  return list;
}

Is there a way to make the line (values[i] == value) use a wildcard?

Nope
  • 22,147
  • 7
  • 47
  • 72
Bill
  • 1,423
  • 2
  • 27
  • 51

3 Answers3

7

Use /[^,]*non,|,[^,]*non/g:

id = '2345,45678,3333non,489,2333non';

console.log(
  id.replace(/[^,]*non,|,[^,]*non/g, '')
)

As a function:

id = '2345,45678,3333non,489,2333non';

removeItem = function(s, ends) {
  pat = new RegExp(`[^,]*${ends},|,[^,]*${ends}`, 'g')
  return s.replace(pat, '')
}

console.log(removeItem(id, 'non'))
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • love the single line of id.replace(/[^,]*non,|,[^,]*non/g, '') How do I change the line to get only the non's now that you made it so easy for me. I don't use RegEx very much – Bill Mar 29 '18 at 14:20
  • Your regex could get a bit shorter `,?\d+non` – revo Mar 29 '18 at 14:33
  • A little clunky, but `id.replace(/([^,]*non,|,[^,]*non)|([^,]*,|,[^,]*)/g, '$1')` should do. @user1314159 – Psidom Mar 29 '18 at 14:33
  • @revo That would leave a comma at the beginning of string, if the string starts with a pattern, e.g. `123non,123,234`. – Psidom Mar 29 '18 at 14:34
  • 1
    Fantastic- leading comma is not a problem THANK YOU – Bill Mar 29 '18 at 14:49
  • I hate to ask but If I changed my ID string to '2345,45678,NON333,489,NON2333' What would the Regex be for the id.replace(/[^,]*non,|,[^,]*non/g, '') I change the spot for the id.replace(/[^,]non*,|,[^,]non*/g, '') but that didn't work. Thanks – Bill Mar 30 '18 at 01:18
  • For that case you can use `id.replace(/non[^,]*,|,non[^,]*/gi, '')`. `[^,]*` matches a non comma pattern so they need to be moved together. – Psidom Mar 30 '18 at 13:27
  • Thank you very much and this works great but misses the last NON if it is the last value. I can add a comma at the end and it will catch it and then remove the comma if need be. What are your thoughts- I do appreciate your input – Bill Mar 30 '18 at 18:13
3

You can also get that result without using regex like this:

var id = '2345,45678,3333non,489,2333non';  
var resArray = id.split(',').filter((item) => item.indexOf('non') === -1);
var resString = resArray.toString();
console.log(resString);

If you do not want to use arrow funtion:

var id = '2345,45678,3333non,489,2333non';  
var resArray = id.split(',').filter(function(item) {
   return item.indexOf('non') === -1;
});
var resString = resArray.toString();
console.log(resString);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • @OP Note that Arrow Functions are not supported in IE and semi-supported in Edge. [**Arrow Function - Browser Compatibility**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibility) – Nope Mar 29 '18 at 13:58
2

You don't need regex for this. Just split on , and filter the array for all elements that don't end in non.

var id = '2345,45678,3333non,489,2333non'
console.log(id.split(',').filter(x => !x.endsWith('non')).join(','))

Thanks to Nope for pointing out that endsWith() will not work in IE. To get around this issue, see Mozilla's Polyfill for endsWith or JavaScript endsWith is not working in IEv10.

ctwheels
  • 21,901
  • 9
  • 42
  • 77