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";

str.match(FSCmask);

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

V/r,

:slight_smile:

parenthesizes

/^(\d{4}|\w{4})$/i

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!

V/r,

:slight_smile:

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

:smile:

Yup… that nailed it. Thanks!

1 Like

http://www.regexr.com/ is really helpful!

I use regex101.com (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.

V/r,

:slight_smile:

Regex101.com works, here! Very nice. Thanks!

:slight_smile:

1 Like

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