regex match dupe

Isogram
Isogram in JavaScript
export function isIsogram(word) {
  return !/([a-z]).*?\1/i.test(word);
}

In the next part of the pattern .*?:

  • . looks for another character which is not a line terminator.
  • * looks for zero or more of those characters.
  • ? looks for as few of those characters as possible, until

\1 finds a repeat of the first captured character. If no repeat is found for the first captured character, then the regex tries again and matches the next a-z as the first captured character, and so on.

The i at the end of the pattern means to ignore case when matching, so [a-z] will also find [A-Z].

The ! at the beginning of the pattern is the logical NOT operator. The function returns true if the regex does not find a repeated letter.

20th Nov 2024 · Found it useful?