Created
December 10, 2018 20:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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