Palindrome code questions

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()){
    return true;
    return false;



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