function isIsogram(str){
return !/(\w).*\1/i.test(str)
}
especially the !/(\w).*\1/i.test(str)
part
function isIsogram(str){
return !/(\w).*\1/i.test(str)
}
especially the !/(\w).*\1/i.test(str)
part
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 againThe /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.