0

I need help with a Regex expression to valid an ID pattern which is 7 characters where the first position is Alpha (A-Z) and ignore case and last 6 is Numeric (0-9).

Example: X155230 or x155230

Thanks guys.

0

4 Answers 4

5

This should work:

/^[a-z]\d{6}$/i
Sign up to request clarification or add additional context in comments.

3 Comments

@ridgerunner - They're the same thing. I don't like the idea of "dumbing down" the \d and /i switch, however. That's fine for explaining what this does, but people will never learn if everything is dumbed down.
@cwolves: Its not a matter of dumbing down. Its a matter of efficiency (a regex is typically faster without the i flag), and accuracy (the \d in many regex engines means more than 0-9, it can include extra unicode digits from other languages.) That said, these differences are likely insignificant in this example, and either expression will work just fine.
fair enough, except that \d in JavaScript at least only matches 0-9 and many browsers have no performance difference (though some do)
1

You could at least do some basic research in the javascript documentation. The pattern you need is

/^[A-Za-z]\d{6}$/

Comments

0

This seems to find your pattern, RegExr, you just have to put it into JavaScript.

The actual Regex /^[a-zA-Z]\d{6}/i

1 Comment

You should definitely put the pattern into your answer as external links go dead.
-1

It's quite simple:

/^[A-Z]{1}[0-9]{6}$/i

3 Comments

@cwolves is completely correct, but I don't think that means this deserves a minus 1.
+1 to undo the -1: I don't either, it's just unnecessary, not harmful :)
@cwolves @Renesis I was waiting for the downvotes to start. Oh well, I can spare the 2 points for being explicit. Thanks for the vote of confidence.

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.