-3

function isIsogram(str){ 
  return !/(\w).*\1/i.test(str)
}

especially the !/(\w).*\1/i.test(str) part

  • See [What does this symbol mean in JavaScript?](/q/9549780/4642212), the docs on MDN about [expressions and operators](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators) and [statements](//developer.mozilla.org/docs/Web/JavaScript/Reference/Statements), [What does this regex mean?](/q/22937618/4642212), the [regex tag wiki](/tags/regex/info), use regex debuggers like [RegEx101](//regex101.com/), read the [ES spec](//tc39.es/ecma262), the [JS docs](//developer.mozilla.org/docs/Web/JavaScript/Reference), and [JS tutorials](//developer.mozilla.org/docs/Web/JavaScript/Guide). – Sebastian Simon Aug 08 '21 at 22:39
  • Test the regex on https://regex101.com/r/nkx56s/1 In the aside panel there's all you need to know. After you see what is matched, and since `.test()` returns a boolean, `!boolean` is just the inverse. If your function was named say `isNotIsogram` than the leading *Logical Not* operator `!` would be unnecessary. – Roko C. Buljan Aug 08 '21 at 22:42

1 Answers1

1

An isogram is a word where every letter only appears at most once.

The regex /(\w).*\1/i can be split into 3 parts:

  • (\w): Find any "word character", i.e. alphanumeric character (or underscore) and save it as a group. This is the first group, so group 1.
  • .*: Match any amount of characters, doesn't matter which (except newlines)
  • \1: Match the first group again

The /i means the regex is case insensitive. The end result is that this RegEx checks whether it can find one letter (matched by \w) and can find the exact same letter again later (matched by \1).

The regex.test(str) just means "does this string match this regex?" while ! inverts the result, so in the end, you have return (not "is there a letter that appears twice?"), aka an isogram.

Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31