RegEx which tests if sentence contain every letter from polish alphabet

Viewed 135
function isPangram(sentence) {
  const polishSpecial = /[śćóźąęłżń]/;
  const RegEx = /(\w).*\1/;
  return !RegEx.test(sentence);
}

Mine regular expression only checks whether the letter is repeating itself. I want also add these polish special characters to the main RegEx.

The point is that sentence can only have one letter from [a-z] and [śćóźąęłżń], then it's true. If sentence doesn't have even one letter from [a-z] or [śćóźąęłżń], then it's false.

1 Answers

You can use

const ContainsAllPolishLetters = (str) => 
 { 
   return [...new Set(str.match(/[A-Za-zżźćńółęąśŻŹĆĄŚĘŁÓŃ]/g))].length == 64;
}
console.log(ContainsAllPolishLetters("...AaĄąBbCcĆćDdEeĘęFfGgHhIiJjKkLlŁłMmNnŃńOoÓóPpRrSsŚśTtUuWwYyZzŹźŻż..."));
console.log(ContainsAllPolishLetters("...A tu mamy za mało polskich liter..."));

Details:

  • .match(/[A-Za-zżźćńółęąśŻŹĆĄŚĘŁÓŃ]/g - extracts all Polish letters from a str string
  • [...new Set(<result_of_Step_1>)] - removes duplicate letters from the array
  • <result_of_the_above>.length == 64 - checks if the count of unique letters is equal to 64, 32 lower- and 32 uppercase Polish letter count. If yes, the return value is true, otherwise, false.
Related