Case insensitive - "SyntaxError: Invalid regular expression"

406 views Asked by At

I'm building an Angular webpage with top menu with a search input giving me some issues... I'm using RESTHeart API in order to build the query for it. In the query sent to the API I'm sending the following:

[a-zA-Z0-9.@-]{4,99}

This pattern should make sure the search input is case insensitive and this was not working well, so I looked at the documentation here https://restheart.org/learn/query-documents/ and the regex should include (?i) in order to be interpreted as "case insensitive". So I did - this is the pattern being sent:

(?i)[a-zA-Z0-9.@-]{4,99}

Now I get an error on the menu loading:

ERROR SyntaxError: Invalid regular expression: /^(?i)[a-zA-Z0-9.@-]{4,99}$/: Invalid group

I also tried hardcoding the (?i) part since it seems that the problem is in that particular part...

Anyone has any idea why this could be happening? I tried https://regex101.com/r/o684Hu/2/ and it works there...

Thank you for your time,

Eunito.

1

There are 1 answers

0
Andrea Di Cesare On BEST ANSWER

In mongodb the $regex query operator allows options

With RESTHeart the query filter should be as follows for case insensitive regex query

GET /db/coll?filter={"field":{"$regex": "[a-zA-Z0-9.@-]{4,99}", "$options": "i"}}

for more info, check the options available for use with regular expression in mongodb documentation.