1

I am trying to create an Angular filter to provide regex functionality. I am consuming the flickr API - see example here and one key value of the json object returned is

{......
   "author": "[email protected] (John Doe)"
....}

Currently, this is my filter regex function:

app.filter('regex', function() {
    return function(input, regex) {
        return input.match(regex);  
    }
})

and in my html I have this

<p>{{user.author | regex:'(/\((.*?))\)/g)'}}</p>

and the filter is injected into the controller, which looks like this

app.controller('testCtrl', function ($http, regexFilter) {
      //do controller stuff!
}

I am looking to isolate just the user's name and drop the [email protected] so that John Doe is returned

Any guidance on how to implement this would be greatly appreciated.

Thanks!

1 Answer 1

6

You could try adding a filter like this

var myApp = angular.module('myApp', []);
myApp.filter('regex', function() {
   return function(val){
     var RegExp = /\(([^)]+)\)/;
     var match = RegExp.exec(val);
     return match[1];
   };
});
myApp.controller('ctrl', function($scope){
    $scope.user = {'author': "[email protected] (John Doe)"};
});

Working JSFiddle here http://jsfiddle.net/WfuAh/147/

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

1 Comment

BOOM! That worked, thank you! You can use it in the html also like this <p>{{user.author | regex}}</p>

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.