Format UK postcode with regex

by Graeme Bryson

HTML

<input id="customerPostcode" type="text">

JavaScript

jQuery(function($) {
  $("#customerPostcode").keyup(function() {
		const regex = /^(.*)(\d)/g;
		const str = $("#customerPostcode").val();
		const subst = `$1 $2`;
		const result = str.replace(regex, subst);
		$("#customerPostcode").val(result);
	});
});