regex - JavaScript Check for palindrome (spaces & punctuation included) -
trying check palindromes. i've checked other answers none included punctuation & spaces.
"never odd or even" , "a man, plan, canal. panama" should return true don't.
function palindrome(str) { if(str.replace(/[^\w\s]|_/g, "").tolowercase() === str.replace(/[^\w\s]|_/g, "").tolowercase().split("").reverse().join("")){ return true; } else { return false; } } palindrome("eye");
i think have error in regex. if want remove spaces, don't need \s
. try changing:
str.replace(/[^\w\s]|_/g, "")
with
str.replace(/[^\w]|_/g, "")
Comments
Post a Comment