最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Regex to remove any spaces, special characters and numbers - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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]/, '')
发布评论

评论列表(0)

  1. 暂无评论