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

How to get 4 sequetial numbers for postcode in javascript from a string?

我想弄清楚如何从字符串中获取 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)));

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

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