Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9281069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:01:38+00:00 2026-06-18T18:01:38+00:00

Currently I have a JavaScript program that is using Regex to detect UK postcodes.

  • 0

Currently I have a JavaScript program that is using Regex to detect UK postcodes.
At the minute it is just displaying them in an alert.
I am currently struggling to detect Postcodes with a space.
For example SW12 5BV, BR3 8DD
It also needs to accept Postcodes without spaces such as DE148NV, JN24HH.
Current Regex Code:

var postcodePattern = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
                var resultPost = postcodePattern.test(words[i]);
                if(resultPost)  {   
                alert("Postcode detected: " + words[i]);

I have also tried various other UK postcode regex which to no avail.
Example

//var postcodePattern = /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR ?0AA)$/

I also have a function which removes all HTML Tags after the regex has taken place which has affected previous regex.

removeHTMLTags: function(aString){

        var strInputCode = aString;

        var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, " ");
        strTagStrippedText = strTagStrippedText.replace(/&nbsp/g," ");

        //Remove some escape characters
        strTagStrippedText = strTagStrippedText.replace(/\(|\)|\{|\}|\[|\]|\?|\*|\+|\||\//ig," ");

        //Replace multilple white spaces with single white space
        strTagStrippedText = strTagStrippedText.replace(/\s+/g," ");

        return strTagStrippedText; },

Any help would be grateful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:01:40+00:00Added an answer on June 18, 2026 at 6:01 pm

    As simple as putting your space inside a character class? I am assuming you are only allowing a single space based on the re-formatting code in the second code block.

    var postcodePattern = /[A-Z]{1,2}[0-9]{1,2}[ ]{0,1}[0-9][A-Z]{2}/i;
                var resultPost = postcodePattern.test(words[i]);
                if(resultPost)  {   
                alert("Postcode detected: " + words[i]);
    

    As per the comments, you are splitting you input on spaces as well here:

    bodyContentsStrippedTags.split(/\s+|\,|\&|\||\/|\?|\!|\:|;|"/);
    

    with the \s+ in your split regex. This means postal codes with a space get passed to your regex as two separate array items and not a single string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a Javascript function that uses a string to reference an object
We currently have a tool on our website that is created by JavaScript. The
I am currently trying to pass an array that I have created in Javascript
I'm currently using JavaScript (CommonJS) in Titanium Studio and have a question about prototyping.
I have JavaScript code that adds and removes classes using the classList object. The
I currently have some JavaScript code that calls a .php file to send me
Currently I have implemented a Javascript Ajax search where if user types a city
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have currently two circles in a <canvas> tag with HTML5 & JavaScript. Now
I'm currently refactoring some Javascript code we have and amongst other things I've changed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.