Check for Palindromes replace method

Tell us what’s happening:
I have solved the problem but I am not sure if this is the right way or could be a better or right way to do it.
I searched for all non-alphanumeric char and some said /\W/g would do it. but this underscore _ still would not replaced. the Rest as you can see.
Please review and would love to hear your input.
Your code so far

function palindrome(str) {
  
  var str0 = str.replace(/\W/g, '').replace(/_/g, '').replace(' ', '').toLowerCase();
  var str1 = str0.split('').reverse().join('');
  return str0 === str1;
}



palindrome("eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36.

Link to the challenge:

The third replace is redundant, since all spaces are already taken care of by the first replace.

There’s nothing wrong with chaining replace calls, but you can combine them into one replace call:

str.replace(/\W|_/g, '').toLowerCase();

Anyway, your code is good and done “right” enough IMO.

1 Like

oh that seems easier solution. thanks