At the moment I have the following $scope.user.username.replace(/[\s]/g, '');
This removes and special characters and spaces, I need to add in the ability to prevent numbers and special characters too but I can't quite grasp how Regex works.
Would someone be able to help me out?
At the moment I have the following $scope.user.username.replace(/[\s]/g, '');
This removes and special characters and spaces, I need to add in the ability to prevent numbers and special characters too but I can't quite grasp how Regex works.
Would someone be able to help me out?
Share Improve this question asked Sep 26, 2014 at 11:29 ngplaygroundngplayground 21.7k37 gold badges98 silver badges174 bronze badges 1- Actually your regex remove only white spaces. – Toto Commented Sep 26, 2014 at 11:35
2 Answers
Reset to default 3You should just be able to use not
in the matching group and remove everything that isn't a letter:
/[^a-zA-Z]/g
DEMO
Just add the characters you want to remove in the character class:
replace(/[\s.;,?%0-9]/, '')