Efficient regex for Canadian postal code function

前端 未结 7 1439
说谎
说谎 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:03

    regex = new RegExp(/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i);
    if(regex.test(value))
        return true;
    else
        return false;
    

    This is a shorter version of the original problem, where value is any text value. Furthermore, there is no need to test for value length.

提交回复
热议问题