Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapthedingbat/ae973dc47ec112dd2a93 to your computer and use it in GitHub Desktop.
Save zapthedingbat/ae973dc47ec112dd2a93 to your computer and use it in GitHub Desktop.
Format a UK postcode to include spaces that may have been omitted.
function formatPostcode(postcode){
return(
//if character fourth from the right is not space
postcode.slice(-4,-3) !== " "
//insert space three characters from the right
?postcode.slice(0,-3) + " " + postcode.slice(-3)
//otherwise use origional
:postcode
)
// upper case
.toUpperCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment