Search code examples
javascriptangularjsregexangularjs-filter

How to remove tab character (\t) from string using angularjs filter?


I have a html data received from server as a string i wanted to remove \t tabs character from content how can i resolve this problem using angularjs filter ? With below code i did not see \t remove from content when it renders to UI.

filter.js

angular.module('App').filter('trusted', ['$sce', function ($sce) {
        return function(url) {
            var content = url.toString().replace('\t', '').split('\r\n');
            return $sce.trustAsResourceUrl(content);
        };
    }]);

main.html

<tr ng-repeat="item in showMessages | filter:searchStr">
    <td>{{item.filename}}</td>
    <td class="serverResults" ng-bind-html="item.value | highlight:searchStr| trusted">{{item.value}}</td>
</tr>

data

<p style=\"text-align: left; font-size: 15px; line-height: 20px; font-family:ClearviewATT, Arial; color: #444444; margin:0; padding:0;\">\t\t\t\t\t\t\t\t\t\t\tError Code: \t\t\t\t\t\t\t\t\t\t\t[(*ErrorCode*)]\t\t\t\t\t\t\t\t\t\t</p>

Solution

  • In your filter you need to use a regular expression

    var content = url.toString().replace(/\\t/g, '').split('\r\n').toString();
    

    otherwise only the first \t (first occurence) will be removed

    And if you want to remove tabulation the regexp will be (no double \ ) :

    var content = url.toString().replace(/\t/g, '').split('\r\n').toString();
    

    You can verify the regular expression here

    After a try in https://jsfiddle.net it seems that replace(/\t/g, '') replace \t and also tabulations.