Regex golf, is this a valid PIN?

David Landry dlandry at byu.net
Mon Feb 24 07:34:52 MST 2014


On Mon, Feb 24, 2014 at 9:33 AM, David Landry <dlandry at byu.net> wrote:

> There's a little bit of redundancy in there. Also, there's no length
> requirement (e.g. isPIN('') also returns true).
>
> Without adding length requirements, I'd do it like this:
>
>   function isPIN(str) {
>     return /^\+?(\d*)$/.test(str);
>   }
>
> With a length requirement, it might look like this (if PINs must be 4
> numbers long, {a,b} for a range from a to b length):
>
>   function isPIN(str) {
>     return /^\+?(\d{4})$/.test(str);
>   }
>

Ooops, missed this cruft, the \+? is redundant too. So,

  function isPIN(str) {
    return /^(\d{4})$/.test(str);
  }


---
David Landry


More information about the PLUG mailing list