admin管理员组

文章数量:1317909

I need a regex which check the string contains only A-Z, a-z and special characters but not digits i.e. (0-9). Any help is appreciated.

I need a regex which check the string contains only A-Z, a-z and special characters but not digits i.e. (0-9). Any help is appreciated.

Share Improve this question edited Jun 3, 2011 at 2:08 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked May 31, 2011 at 12:58 SalilSalil 47.5k22 gold badges125 silver badges160 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You can try with this regex:

^[^\d]*$

And sample:

var str = 'test123';
if ( str.match(/^[^\d]*$/) ) {
  alert('matches');
}

Simple:

/^\D*$/

It means, any number of not-a-digit characters. See it in action…

The alternative is to reverse your test. Just check if there's a digit present, using the trivial:

/\d/

…and if that matches, your string fails.

You're looking for a character class: ^[A-Za-z.,!@#$%^&*()=+_-]+$.

The ^ and $ anchor the regex by marching the beginning and end of the string, respectively.

what about:

var re = /^[a-zA-Z!#$%]+$/;

Fell free to add any special character you need inside the character class

本文标签: regexhow to check digits present in javascript string or notStack Overflow