1

I am trying to figure out how to get 4 sequential numbers from a string which will be a postcode from the string. The regex seems to work fine in regex 101 but when I try to use it in javascript I get an error that reads Uncaught SyntaxError: Unexpected token ?

My code is

var str = "1 George Road, Sydney  NSW 2000"; 
console.log(str.match(((?<!\d)(\d{4}(\d{4})?)\b)));
Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
kevinabraham
  • 1,378
  • 4
  • 28
  • 55
  • besides your lack of `//` ... you have `pcre (php)` checked on regex 101 - so, that won't help checking javascript regexp - and JS regexp doesn't "support" negative lookbehind – Jaromanda X Feb 16 '18 at 03:24

1 Answers1

1

You must include //g on regex for example /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>
4b0
  • 21,981
  • 30
  • 95
  • 142