Efficient regex for Canadian postal code function

前端 未结 7 1387
说谎
说谎 2020-12-24 01:57
var regex = /[A-Za-z]\\d[A-Za-z] ?\\d[A-Za-z]\\d/;
var match = regex.exec(value);
if (match){
    if ( (value.indexOf(\"-\") !== -1 || value.indexOf(\" \") !== -1 )          


        
相关标签:
7条回答
  • 2020-12-24 02:24

    User kind, postal code strict, most efficient format:

    /^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i
    

    Allows:

    • h2t-1b8
    • h2z 1b8
    • H2Z1B8

    Disallows:

    • Z2T 1B8 (leading Z)
    • H2T 1O3 (contains O)

    Leading Z,W or to contain D, F, I, O, Q or U

    0 讨论(0)
提交回复
热议问题