创建一个 angular 正则表达式过滤器

create an angular regex filter

我正在尝试创建一个 Angular 过滤器来提供正则表达式功能。我正在使用 flickr API - see example here 并且返回的 json 对象的一个​​键值是

{......
   "author": "nobody@flickr.com (John Doe)"
....}

目前,这是我的 filter 正则表达式函数:

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

在我的 html 我有这个

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

并将过滤器注入控制器,看起来像这样

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

我希望仅隔离用户名并删除 nobody@flickr.com 以便返回 John Doe

任何有关如何实施此方法的指导将不胜感激。

谢谢!

您可以尝试添加这样的过滤器

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': "nobody@flickr.com (John Doe)"};
});

在这里工作 JSFiddle http://jsfiddle.net/WfuAh/147/