0

I have list of song titles like these

And Can It Be That I Should Gain?

Problem is when i do an exact case insensitive search ? is treated as regex character and i dont get search results as expected.

So how do i encode song title so title itslef is not treated as regex

Please advise

{
  "data.title": {
    "$regex": "^And Can It Be That I Should Gain?$",
    "$options": "i"
  }
}
  • How to deal with special characters (in regex): https://stackoverflow.com/questions/27721008/how-do-i-deal-with-special-characters-like-in-my-regex – prasad_ Mar 14 '22 at 06:22
  • Just escape the question mark ``\?``. – Marc Sances Mar 14 '22 at 10:53
  • You can escape with backslash. `\?` Why are you using regex for an exact match for the whole line? –  Mar 14 '22 at 10:56
  • I generate this JSON string in C#, and I wont know what kind of spcial chars Title will have? I;ll use this and see if it solves the problem Regex.Escape(String) – Rehan ul Hassan Mar 14 '22 at 19:46

0 Answers0