-3

I'm trying to get the major part of a UK postcode. For example, the following are expected...

M34EF -> M3

M3 4EF -> M3

I already have the postcode (and just the postcode) in a JavaScript variable. Based on a regex I found here, I thought the following should work...

var regex = /^[A-Z0-9]{3}([A-Z0-9](?=\s*[A-Z0-9]{3}|$))?/;
var major = regex.match(postcode);

...but this gives the error shown above.

What am I doing wrong? Or, is there a better way to do this? Thanks

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106

1 Answers1

2

Because .match should be called on your postcode string.

postcode.match(regex)

It is String.prototype method. Check here

Ihor Yanovchyk
  • 768
  • 6
  • 13
  • Thanks for that. The reason I did it that way was because on the preceding lines (not shown as they were working), where I validated the postcode as a whole, I did exactly what I showed, although I was calling `text` rather than `match`. Anyway, works now, thanks – Avrohom Yisroel Jul 01 '21 at 14:01