-1

I have a small worry with RegExp in JavaScript. I would like to make an RegExp that is insensitive to accents.

For example if I do:

var str = 'césar'; 

var i = New Regexp (str, 'desired parameter, similar to <i> for example').exec('cesar'); 

console.log(i) // césar or cesar should print

Does something like this exist?

kind26
  • 7
  • 1
  • 7
  • 1
    I believe that the question is slightly different. In this case we have a known word where an individual letter may be substituted by another letter with a diacritical mark. The question flagged as a duplicate is for allowing character ranges to still match. – BlueWater86 Jan 30 '19 at 01:05

1 Answers1

1

There is no RegExp parameter that you can pass to alter the way accents are treated. What you would need to do is build up a matrix of characters that should substitute each other and then construct a RegExp pattern from these substitute characters.

const e = ['È', 'É', 'Ê', 'Ë', 'è', 'é', 'ê', 'ë'],
    a = ['à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ']

const substitutions = {
  e, E: e, a, A: a
}

var str = 'Cesar';
const pattern = Array.from(str).map(c => substitutions[c] ? `[${c}${substitutions[c].join("")}]`: c).join("")
console.log(pattern)

var i = new RegExp(pattern, "gi").exec('césar');
console.log(i)
BlueWater86
  • 1,773
  • 12
  • 22