javascript - filters with wildcards in angularjs -
i'm trying filter list of values wildcards.
searchstring = "ab*cd"
should return values start "ab" , end "cd".
so far i've tried split string in multiple substrings asterisk , search word starts first substring , simultaneously end second 1 (or 1 of two, depending on position of asterisk).
i'm asking if there better method of doing this.
this code search strings start searchstring:
function escaperegexp(string){ return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'); } $scope.query = ''; var regex; $scope.$watch('query', function (value) { regex = new regexp('\\b' + escaperegexp(value), 'i'); } $scope.filterbysearch = function(article) { if (!$scope.query) return true; return regex.test(article.title); };
write custom filter like:
iapp.filter('myfilter', function() { return function( items, types) { var filtered = []; angular.foreach(items, function(item) { if(types.wildcard == false || item.name.match(types.value)) { filtered.push(item); } }); return filtered; }; });
where filter should be:
$scope.types = {wildcard:false, value: /^ab.*cd$/};
html example
<tr data-ng-repeat="value in values | myfilter:types">
demo plunker
Comments
Post a Comment