-1

I would like to remove every non alphanumeric character from a word, however I rather keep whitespaces. Is it able to somehow join these two cases together?

const word = 'dwa$-I| awd#4'.replace(/\W/g, '');
console.log(word); // whitespace removed =((
aloisdg
  • 19,231
  • 5
  • 81
  • 97
Patrickkx
  • 1,470
  • 6
  • 26
  • 53

1 Answers1

1

You can use [^\w\s] which means you're excluding alpha-numeric and space character from the search.

const word = 'dwa$-I| awd#4'.replace(/[^\w\s]/g, '');
console.log(word);
vrintle
  • 5,315
  • 1
  • 11
  • 42