1

I have this code:

<!DOCTYPE html>
<html>
    <head>
        <script>
            window.onload = function() {
                var src = document.getElementById("one"),
                    dst = document.getElementById("two");
                src.addEventListener('input', function() {
                    dst.value = src.value.replace(/\D/g,'');
                });
            };
        </script>
    </head>
    <body>
    
        <input type="text" id="one" name="one">
        <input type="text" id="two" name="two">
    
    </body>
</html>

This will remove everything that isn't a digit from one and insert in two.

But I want to keep only a string of five digits side by side.

Means, if one is:

test 1 test 1234 test; no output

test 1 12345 test; output 12345

test 123456 test 12 test; no output

12394 test; output 12394

84725; output 84725

Does anybody know how to change the REGEX to reach this?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Hi David, I believe you deleted your old question and reposted this one. You’re trying to parse the zip/postal code from an address field. While your suggestion may work, here are a few caveats: 1. 5 digit house numbers (i.e. `12345 Some street`); 2. Zip codes have different formats in different countries (i.e. in Canada: `A0A 0A0`) – ctwheels Sep 24 '17 at 15:13
  • You really should look at this question about addresses and databases so that you can properly organize your address code: https://stackoverflow.com/questions/310540/best-practices-for-storing-postal-addresses-in-a-database-rdbms you **WILL** run into issues if you decide to make it US only and if you use regex to parse zip codes, that’s a **guarantee** – ctwheels Sep 24 '17 at 15:20
  • @ctwheels I already do. I have different columns in ma database for address and zip. This code is for the "insert into database" form. Normally I copy the zip manually in the zip text field. Now I'll have less work. Do you understand what I mean? –  Sep 25 '17 at 08:28

3 Answers3

2

You can use \d to match digits, and specify the length of your choice with {5}. Then, word boundaries (\b) make sure that there is no other digit adjacent to you matches:

/\b\d{5}\b/g

JJWesterkamp
  • 7,559
  • 1
  • 22
  • 28
  • This can be shortened to `\b\d{5}\b` - the `[]` isn’t necessary since the set only contains one value – ctwheels Sep 24 '17 at 15:24
0

For your specific use case you can use \b(\d{5})\b/g to capture the required values in the capture group. Otherwise the matched values will have spaces.

nilobarp
  • 3,806
  • 2
  • 29
  • 37
  • This is incorrect due to the fact that there may or may not be a space next to the digits. The correct answer is to use `\b` instead of `\s` since it can be found at the beginning or end of a string (nothing before/after it), it could have a comma or some other non-word character following it as well. – ctwheels Sep 24 '17 at 15:27
0

Solution:

<!DOCTYPE html>
<html>
<head>
<script>
 window.onload = function() {
  var src = document.getElementById("one"),
  dst = document.getElementById("two");
  src.addEventListener('input', function() {
   dst.value = src.value.match(/\b\d{5}\b/g);
});
};
</script>
</head>
<body>

<input type="text" id="one" name="one">
<input type="text" id="two" name="two">

</body>
</html>