Palindrome code questions

Palindrome code questions
0

#1

While completing this challenge, I got stuck as I was unable to find any documentation as how to replace the “_”. I searched multiple places on the web and could not find good documentation. I have also attached my code which is a bit lengthy but still got the job done.

Why does this code work: var strReplace = str.replace(/[\W_]/g,’’);

And why does this code not work: var strReplace = str.replace(/\W+/g,’’);

Code Below:

function palindrome(str){

//var re = /\W+/g;

var strReplace = str.replace(/[\W_]/g,'');
//var stre = str;
var arraySplit = [];
var arrayReverse = [];
var joinReverse = '';

arraySplit = strReplace.split("");
arrayReverse = arraySplit.reverse();
joinReverse = arrayReverse.join("");


if(strReplace.toLowerCase() === joinReverse.toLowerCase()){
    console.log(joinReverse.toLowerCase());
    console.log(strReplace.toLowerCase());
    return true;
}
else{
    console.log(strReplace);
    console.log(joinReverse);
    return false;
}

}

palindrome(“Race_Car”);


#2

\w matches alphanumeric characters and underscores. So it’s opposite, \W, matches anything that’s not alphanumeric or an underscore.