Skip to content

Instantly share code, notes, and snippets.

@iCaspar
Created December 10, 2018 20:24
Show Gist options
  • Save iCaspar/457cdb9ea9bb5f2ad0fed7203bd81c10 to your computer and use it in GitHub Desktop.
Save iCaspar/457cdb9ea9bb5f2ad0fed7203bd81c10 to your computer and use it in GitHub Desktop.
A function to check whether a string, stripped of non alphanumeric characters and spaces, is a palendrome
function palindrome(str) {
const lettersOnlyRegex = /[a-zA-Z0-9]/g
const letters = str.match(lettersOnlyRegex)
if (null === letters || 0 === letters.length) {
return false
}
const strippedString = str.match(lettersOnlyRegex).join('').toLowerCase()
let reverse = ''
let i = strippedString.length
while (i-- > 0) {
reverse += strippedString[i]
}
return reverse === strippedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment