skip to Main Content

In JavaScript, I would like to remove accent and diacritic only on capital letters:

  • À => A
  • Î => I
  • Ï => I
  • Ô => O

I need to keep the lower case letters:

  • à => à
  • î => î

Is there a better way than testing each letter individually?

2

Answers


  1. This is still kind of checking every letter, but I would just create a function that uses regex to do this, as it makes it much easier to manage.

    For example

    function removeAccents(text) {
      return text.replace(/[ÀÁÂÃÄÅ]/g, 'A')
        .replace(/[ÈÉÊË]/g, 'E')
        .replace(/[ÌÍÎÏ]/g, 'I')
        .replace(/[ÒÓÔÕÖ]/g, 'O');
        // Add more replacements as needed
    }
    
    Login or Signup to reply.
  2. Remove diacritics from cappital letters using regex and normalize

    function removeAccentsFromCapitalLetters(text) {
      return text.replace(/[ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ]/g, function(match) {
        return match.normalize("NFD").replace(/[u0300-u036f]/g, "");
      });
    }
    
    const inputText = "Héllo Wórld ÀÈÌÒÙ";
    console.log(removeAccentsFromCapitalLetters(inputText));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search