Regular expression search

1

I'm trying to do a search with regular expressions in an immense json this way:

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

But it is not working. The goal is to make the search as if it were LIKE %valor% of mysql.

    
asked by anonymous 11.03.2015 / 19:40

1 answer

2

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

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

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

    
11.03.2015 / 20:14