-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?

1
  • 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. Commented Jan 30, 2019 at 1:05

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.