0

I need to apply a regex to strip all non-digits from an object's value in an ng-repeat.

This is what I'm trying to do:

   <span>{{obj.value.replace(/\D/g, '')}}</span>

Any idea on how to do this best?

Federico
  • 6,388
  • 6
  • 35
  • 43

1 Answers1

2

You can do it that way, but the best way would probably be to create a custom filter.

app.filter('onlyNumbers', function() {
  return function(val) {
    return val.replace(/\D/g, '');
  };
});

Then:

<span>{{ obj.value | onlyNumbers }}</span>
Joe
  • 2,596
  • 2
  • 14
  • 11
  • Thanks! worked great. How can I apply that filter when saving that variable? ie `ng-click="number = obj.value;"` ideally it would be `ng-click="number = obj.value|onlyNumbers;"` so that number is ONLY the numeric values from `obj.value` – Federico May 20 '14 at 23:26
  • 1
    Don't quote me on this, but try ng-click="number = (obj.value | onlyNumbers);". – Joe May 20 '14 at 23:29