I'm trying to filter a list of values with wildcards.
searchString = "ab*cd"
Should return all the values that start with "ab" and end with "cd".
So far I've tried to split the string in multiple substrings where the asterisk is and then search all the word that starts with the first substring and simultaneously end with the second one (or one of the two, depending on the position of the asterisk).
I'm asking if there is a better method of doing this.
This is the code to search strings that start with a 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With