1

When using regex to allow for case insensitivity, it only works for plain strings. The db query returns no results when parenthesis or other special characters are used for the name.

Example: "Push It" will work fine and return songs. However when searching "Escape (The Pina Colada Song)", it returns no songs. How can I make the Regex allow for these special characters?

Any suggestions would be great.

const lowercaseName = new RegExp(`^${name}`, 'i')
const songs: any = await Song.find({ name: lowercaseName, artist_id })
1

1 Answer 1

1

You should escape all the special characters in the string that you are sending. Try this:

const lowercaseName = new RegExp(`^${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'i')
const songs: any = await Song.find({ name: lowercaseName, artist_id })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.