Rafael Gallardo
Rafael Gallardo

Reputation: 143

How I can ignore accent sensitive when I use a regex query in Cloudant Query?

Please help me to ignore accent sensitive in a Cloudant Query

I found on the internet a method to ignore case sensitive (?i), it works fine. But doesn't work with accent sensitive.

This is a part of my Cloudant Query:

{
  "modelo": { 
     "$regex": "(?i)sábado" 
   }
}

Thanks!!

Upvotes: 0

Views: 1402

Answers (2)

Rafael Gallardo
Rafael Gallardo

Reputation: 143

I found the solution: Just have to change any vowel in the string you want to search for the respective vowel.

var dat = "sábado"
var res = dat.replace(new RegExp(/[aáAÁ]/g), "[aáAÁ]");

Upvotes: 0

markwatsonatx
markwatsonatx

Reputation: 3501

Like the first commenter mentioned you will probably need to replace the characters in your search. For example, you could change your search to something like this

{
  "$or": [
    {"modelo": {"$regex": "(?i)sábado"}},
    {"modelo": {"$regex": "(?i)sabado"}}
  ]
}

If you have control of the data as it is stored in Cloudant it might be easier if you replace the accent characters on the way in. If you need the original value you could add a new field for searching. For example, you go could set modelo to sábado and then add another field like modelo_search and set the value to sabado. Then whenever you perform your search you use the modelo_search field and replace the accents in the search string, like so:

{
  "modelo_search": { 
    "$regex": "(?i)sabado" 
  }
}

Upvotes: 0

Related Questions