Checking the resources online, I was able to come up with the following, but struggling with the Regular Expression portion in .replace. The underscore is still there
Output = ey_e
function palindrome(str) {
var newstr = str.toLowerCase().replace(/\W/,'');
return newstr;
}
palindrome("#Ey_e");
How come the sample solution shows this RegEx with an underscore \W_? I don’t know what this means and tried looking online
Sample Solution
function palindrome(str) {
return str.replace(/[\W_]/g, '').toLowerCase() ===
str.replace(/[\W_]/g, '').toLowerCase().split('').reverse().join('');
}
You can see the | operator is there. So regex looks for W or _. The sample solution you list doesn’t include the or operator, so that might be problematic.
In a more direct response to your code, I imagine regex would only replace instances of “W_” exactly. Obviously, ey_e doesn’t contain W_, so it wouldn’t do the replacement.
Thanks for the quick response! Just a thing on top I noticed, are square brackets in the sample solution just optional to help the coder clarify what they want to be edited in the regex?