I wanted to write a javascript function to sanitize user input and remove any unwanted and dangerous characters.
It must allow only the following characters:
- Alfanumeric characters (case insentitive): [a-z][0-9].
- Inner whitespace, like "word1 word2".
- Spanish characters (case insentitive): [áéíóúñü].
- Underscore and hyphen [_-].
- Dot and comma [.,].
- Finally, the string must be trimmed with trim().
My first attempt was:
function sanitizeString(str){
str = str.replace(/[^a-z0-9áéíóúñü_-\s\.,]/gim,"");
return str.trim();
}
But if I did:
sanitizeString("word1\nword2")
it returns:
"word1
word2"
So I had to rewrite the function to remove explícitly \t\n\f\r\v\0:
function sanitizeString(str){
str = str.replace(/([^a-z0-9áéíóúñü_-\s\.,]|[\t\n\f\r\v\0])/gim,"");
return str.trim();
}
I'd like to know:
- Is there a better way to sanitize input with javascript?
- Why \n and \t doesn't matches in the first version RegExp?