.replace regex not working

.replace regex not working
0

#1

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
  //reverter
  //comparar com str

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


  return str;
}

var result = palindrome("eYe9_");

console.log(result);

#2

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


#3

I’ve tried this:

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

It doesn’t work either


#4

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


#5

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…


#6

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