0

How can I do something similar to the below

result = $.grep(data, function(e){ return e.firstname == name; });

with having name to be a regex expression, i.e. name starts with "Kevin*"

4

1 回答 1

2

如果没有测试,我建议:

result = $.grep(data, function(e){
             return new RegExp('^Kevin').test(e.firstName);
         });

要使用变量,则可以将上面的内容重写为:

var name = 'Kevin';
result = $.grep(data, function(e){
             return new RegExp('^' + name).test(e.firstName);
         });

参考:

于 2013-08-13T22:34:08.227 回答