Premshankar Tiwari Premshankar Tiwari - 4 months ago 13x
AngularJS Question

JavaScript - Search on formatted numbers

I am showing numbers formatted with comma in my AngularJs app using number filter with a directive to highlight the searched string in the results.


if (phrase) text = text.replace(new RegExp('('+phrase+')', 'gi'), '<span class="highlighted">$1</span>');

When the user types 123, I need to show 1,23,456. It messes up when encounters a comma..

Is there any regexp to achieve this? or any inbuilt angular way..?
Here's the plunk


I changed your highlight-filter to this (add ',?' for each char in phrase):

.filter('highlight', function($sce) {
  return function(text, phrase) {
    if(typeof(text) != 'string') text = text.toString();
    if (phrase) {
      var regexp = '';
      for(var i = 0; i < phrase.length; i++) {
        regexp += phrase[i] + ',?';
      text = text.replace(new RegExp('('+regexp+')', 'gi'),
      '<span class="highlighted">$1</span>')

    return $sce.trustAsHtml(text)

Updated plnkr