0

http://jsfiddle.net/f4Zkm/213/

HTML

<div ng-app>
    <div ng-controller="MyController">
        <input type="search" ng-model="search" placeholder="Search...">
        <ul>
            <li ng-repeat="name in names | filter:filterBySearch">
                {{ name }}
            </li>
        </ul>
    </div>
</div>

app.js

function escapeRegExp(string){
    return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function MyController($scope) {
    $scope.names = [
    'Lolita Dipietro',
    'Annice Guernsey',
    'Gerri Rall',
    'Ginette Pinales',
    'Lon Rondon',
    'Jennine Marcos',
    'Roxann Hooser',
    'Brendon Loth',
    'Ilda Bogdan',
    'Jani Fan',
    'Grace Soller',
    'Everette Costantino',
    'Andy Hume',
    'Omar Davie',
    'Jerrica Hillery',
    'Charline Cogar',
    'Melda Diorio',
    'Rita Abbott',
    'Setsuko Minger',
    'Aretha Paige'];

    $scope.search = '';
    var regex;
    $scope.$watch('search', function (value) {
        regex = new RegExp('\\b' + escapeRegExp(value), 'i');
    });

    $scope.filterBySearch = function(name) {
        if (!$scope.search) return true;
        return regex.test(name);
    };
}

From the above example, I have been trying to create a wildcard regex search by using a special character '*' but I haven't been able to loop through the array.

Current output: If the input is di, it showing all the related matches.

Required: What I am trying to get is, if the input is di*/*(any input), it should show all the matches as per the given input.

2
  • 1
    Please provide some example of the input and the expected output. It's not clear from your question what you want and what is not working. Commented Jan 5, 2017 at 20:52
  • @wdosanjos please check the updated question link. Commented Jan 6, 2017 at 21:15

1 Answer 1

1

There are a couple issues with your approach. First, you are escaping * in your escape routine, so it can not be used by the client.

Second, you are not anchoring your lines, so the match can occur anywhere.

To fix, remove the asterisk from the escape function :

function escapeRegExp(string){
    return string.replace(/([.+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

Then in your watch function replace * with .* and add line anchors :

$scope.$watch('search', function (value) {
        var escaped = escapeRegExp(value);
        var formatted = escaped.replace('*', '.*')

        if(formatted.indexOf('*') === -1){
            formatted = '.*' + formatted + '.*'
        }

        regex = new RegExp('^' + formatted + '$', 'im');
    });

Here is a fiddle

Sign up to request clarification or add additional context in comments.

7 Comments

Hi. Thank you so much for your contribution. Now the normal search is not working. (i.e) "Lolita" is not searching the matches. and the same issues occurs in the second word of the name. When I try to search "Dipietro" it's not come up with any result.
Correct, since you now require a wildcard to match before or after a word. Could you give some examples of what patterns you would expect to match what? I am a bit confused about how you would like the wild card to work
please check the updated question regEx in angular filter.
updated answer and fiddle. If I understand correctly you would like the input to match anywhere when no wildcard is provided.
you are great. This is what I am looking for. Is it possible to updated the same scenario in the [plunker] (plnkr.co/edit/WVdKoiVVZxXtLU7YVESL?p=preview) as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.