Alternatively, you can use regex to simply find all the alphabetic characters that appear at the beginning of the line:
var postcode = 'AA1 2BB'; var postcodePrefix = postcode.match(/^[a-zA-Z]+/);
If you want the leading characters to be non-numeric, you can use:
var postcodePrefix = postcode.match(/^[^0-9]+/);
source share