Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
欧洲或英国的RegEx Jquery邮政编码_Jquery_Regex_Postal Code - Fatal编程技术网

欧洲或英国的RegEx Jquery邮政编码

欧洲或英国的RegEx Jquery邮政编码,jquery,regex,postal-code,Jquery,Regex,Postal Code,尝试允许英国邮政编码(GF433ED)或欧洲邮政编码(12345)返回true或false,但以下代码始终返回false: function valid_postcode(postcode) { postcode = postcode.replace(/\s/g, ""); alert(postcode); var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}|^[0-9]{5}$/; return rege

尝试允许英国邮政编码(GF433ED)或欧洲邮政编码(12345)返回true或false,但以下代码始终返回false:

 function valid_postcode(postcode) {
     postcode = postcode.replace(/\s/g, "");
     alert(postcode);
     var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}|^[0-9]{5}$/;
     return regex.test(postcode); 
 }
任何帮助都将不胜感激


感谢使用
\s*
提供可选空间

/[A-z]{1,2}[0-9]{1,2}\s*[0-9][A-Z]{2}/i

谢谢大家,这两方面都有点困难(忘记发布答案)

见工作正则表达式:


/^请注意,您的英国邮政编码regex不完整。英国政府提供的代码见此:
([Gg][Ii][Rr]0[Aa]{2})(([A-Za-z][0-9]{1,2})(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})([A-Za-z][0-9][A-Za-z][A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])1,2}因为代码不正确。此外,可能需要锚,因为代码似乎需要作为整个字符串进行验证。