如何从字符串中获取javascript中邮政编码的4个序列号?

时间:2018-02-16 03:13:08

标签: javascript regex postal-code

我试图找出如何从字符串中获取4个连续数字,这将是字符串中的邮政编码。 The regex seems to work fine in regex 101但是当我尝试在javascript中使用它时,我收到一条错误,内容为Uncaught SyntaxError: Unexpected token ?

我的代码是

var str = "1 George Road, Sydney  NSW 2000"; 
console.log(str.match(((?<!\d)(\d{4}(\d{4})?)\b)));

1 个答案:

答案 0 :(得分:1)

您必须在正则表达式中包含//g,例如/Your Pattern/g

&#13;
&#13;
var str = "1 George Road, Sydney  NSW 2000"; 
console.log(str.match(/((?<!\d)(\d{4}(\d{4})?)\b)/g));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;