Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Borodin for Reformat string containing uk postcode using regex

If you use the regular expression /^([A-Z]{1,2}\d{1,2}[A-Z]?)\s*(\d[A-Z]{2})$/ you can extract the two parts of the postcode and reassemble them with an intervening space.

var list = ['N13LD', 'EC1A3AD', 'GU348RR'];for (var i = 0; i < list.length; i++) {  var parts = list[i].match(/^([A-Z]{1,2}\d{1,2}[A-Z]?)\s*(\d[A-Z]{2})$/);  parts.shift();  alert(parts.join(''));}

output

N1 3LDEC1A 3ADGU34 8RR

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>