I am currently working on filtering a list of names using regular expressions (regex
).
The data is stored in the following format:
[
{
"firstName": "Jhon",
"lastName": "Doe",
},
...
]
Users have the option to enter either a full name, first name, or last name. However, it is crucial for me to match all variations. My mongo query is structured like this (I utilized Loopback 4
to construct this query, but the concept remains clear)
{
or: [{firstName: new RegExp(searchKey, 'i')}, {lastName: new RegExp(searchKey, 'i')}],
};
Unfortunately, this method does not successfully match when a user enters "jhon doe", yet it does work if only a first or last name is provided.
This leads me to my inquiry: Is there a way to match part of a string against a regex?
The desired outcome is to achieve a successful match for "jhon", "doe", "jhon doe", and "jhondoe".