I need to replace special characters from a string, like this:
this.value = this.value.replace(/\\n/g,\'\');
Except for the regex part, I need
Try:
this.value = this.value.replace(/\w|-/g, '');
Reference:
You can use character class with ^
negation:
this.value = this.value.replace(/[^a-zA-Z0-9_-]/g,'');
Tests:
console.log('Abc054_34-bd'.replace(/[^a-zA-Z0-9_-]/g,'')); // Abc054_34-bd
console.log('Fš 04//4.'.replace(/[^a-zA-Z0-9_-]/g,'')); // F044
So by putting characters in [^...]
, you can decide which characters should be allowed and all others replaced.