Regular expression search

Asked

Viewed 114 times

1

I’m trying to do a regular expression search on a huge json this way:

json = _.filter(dados.responseJSON,function(rst){ 
        return /rst.dst/m == tel
});

But it’s not working. The goal is to search as if LIKE %valor% mysql.

  • You can give an example of rst and what do you want to match? I don’t quite understand what you want to compare with regex.

1 answer

2


You cannot create a regular expression literally by passing a variable as js will not interpret the variable. What you can do is create an object RegExp:

return new RegExp(rst.dst, 'm').test(tel);

Remarks: be careful with the value of this variable, since some characters have a special meaning and must be escaped with \ (for example . and *). Another point, make sure that the variable tel exists in the scope of the function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.