Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check digits present in javascript string or not

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.

like image 884
Salil Avatar asked Feb 22 '26 17:02

Salil


2 Answers

You can try with this regex:

^[^\d]*$

And sample:

var str = 'test123';
if ( str.match(/^[^\d]*$/) ) {
  alert('matches');
}
like image 105
hsz Avatar answered Feb 25 '26 08:02

hsz


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.

like image 39
Gareth Avatar answered Feb 25 '26 06:02

Gareth