RegEx to match either four integers OR four letters

Hello, all,

I’m trying to write a RegEx that will match either four integers (“1234”) or four letters (“ABCD”), but not a mix of letters and numbers.

I’ve got this:

var FSCmask = "/^\d{4}|\w{4}$/i";


But it’s always stating that the input is incorrect regardless of what I enter (“1234”, “ABCD”, “A2C4”). What am I missing?





Keep in mind that this will match ____ and still permits AD09, as \w includes _ and 0-9. If you DO NOT want AD09, you should use [a-z]{4}

“Parenthesizes”?? :smile:

So, just adding parenthesis to the expression should fix this? (Banging head into desk)

I’ll give that a shot. Thanks!

[quote=“cpradio, post:2, topic:190098”]
If you DO NOT want AD09, you should use [a-z]{4}
[/quote]D’oh! Thanks, again!



Yes, as the | operator applies inside parenthesizes. :smile:


Yup… that nailed it. Thanks!

1 Like is really helpful!

I use (but that is just my personal preference) :smile: I also have the book Regular Expressions Pocket Reference by O’Reilly and I highly recommend it. I use it weekly.

RegExr will load, but that’s all it will do. Work network is super-paranoid (civil service). But, thanks for the suggestion.


:slight_smile: works, here! Very nice. Thanks!


1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.