user1781626
user1781626

Reputation:

Using regex with jQuery

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*"

Upvotes: 0

Views: 7515

Answers (1)

David Thomas
David Thomas

Reputation: 253486

Without testing, I'd suggest:

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

To use a variable then the above can be rewritten to:

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

References:

Upvotes: 2

Related Questions