3

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.

4 Answers 4

6

You can try with this regex:

^[^\d]*$

And sample:

var str = 'test123';
if ( str.match(/^[^\d]*$/) ) {
  alert('matches');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Also ^\d equals \D so you can use: ^\D*$
thanks for additional info, it works for me so upvote it.i didn't check for other answers as of now so will accept only after checking all the answers
3

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.

1 Comment

The second regex should be slightly more efficient and is as far as I can tell the best answer.
0

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.

Comments

0

what about:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.