.replace regex not working

Can’t make this work… I tried lots of modifications, including declare regex as a variable and pass it to function…

function palindrome(str) {
  //passar para lower
  //remover chars não alfanuméricos
  //comparar com str

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

  return str;

var result = palindrome("eYe9_");


You escaped the \ so instead of the special token \W you have a \ followed by the letter W

1 Like

I’ve tried this:

str = str.replace(/\W/g,'');

It doesn’t work either

This code is, in fact, removing all characters that do not match the token \W.

1 Like

Yes… and that is what is should do, right? Remove “all non-alphanumeric characters (punctuation, spaces and symbols)”

And I think it is working…

Just dont work with underscores…

This works…

s = s.replace(/[^A-Za-z0-9]/g,'');

Because \W is “Equivalent to [^A-Za-z0-9_].”

I just have to understand how it works… Regex is weird