SSN Regex for 123-45-6789 OR XXX-XX-XXXX

前端 未结 6 1271
长发绾君心
长发绾君心 2020-12-04 17:43

Can someone provide me a regex for SSN that matches either

123-45-6789

OR

XXX-XX-XXXX

I currently have ^\\d{3}-?\\d{2}-?\\d{4}$

6条回答
  •  Happy的楠姐
    2020-12-04 18:20

    Then it can be

    /^[\dX]{3}-?[\dX]{2}-?[\dX]{4}$/
    

    if you want x to be valid too, you can add the i modifier to the end:

    /^[\dX]{3}-?[\dX]{2}-?[\dX]{4}$/i
    

    On second thought, the regex above will accept

    123-xx-xxxx
    

    as well, so depending on whether you want this form to be accepted or not, you can use your original form "or" the other form:

    /^(\d{3}-?\d{2}-?\d{4})|(xxx-xx-xxxx)$/i
    

提交回复
热议问题