我试图找出如何从字符串中获取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)));
答案 0 :(得分:1)
您必须在正则表达式中包含//g
,例如/Your Pattern/g
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;