0

I am trying to make a regex that will match this pattern,

Must be 40 characters long,

Must contain only letters and numbers,

Must contain no spaces,

Case insensitive,

So far I have come up with this but it does not work;

/^[0-9a-f]+$/i

Thanks

4
  • Ahh ok thanks that is where I was going wrong, thanks Commented May 21, 2012 at 13:38
  • The a-f bit is odd. You don't want to match on G through Z? Are you trying to match hexadecimal digits? If so, note that the maximum value of a hexadecimal digit is e (decimal 15), not f (decimal 16 is 10 in hex). Commented May 21, 2012 at 14:09
  • @GarrettAlbright: Actually, f is 15. ;) Commented May 21, 2012 at 15:01
  • …Oh. Now that I count on my third hand, I see you're right. Well, that's embarrassing. Commented May 22, 2012 at 4:15

3 Answers 3

4
/^[0-9a-f]{40}$/i

should do the trick. The number in curly brackets defines the number of characters.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this regex:

/^[\da-z]{40}$/i

If you really only want the letters a-f then use:

/^[\da-f]{40}$/i

1 Comment

\w should also match underscores (_), which I do not think is something the OP is after.
1

Try this:/^[0-9a-z]{40,40}$/i

2 Comments

{40,40} can be replaced by {40}.
A Single number between curly braces denotes an exact number which must be matched :).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.