53
testing= testing.match(/(\d{5})/g);

I'm reading a full html into variable. From the variable, want to grab out all numbers with the pattern of exactly 5 digits. No need to care of whether before/after this digit having other type of words. Just want to make sure whatever that is 5 digit numbers been grabbed out.

However, when I apply it, it not only pull out number with exactly 5 digit, number with more than 5 digits also retrieved...

I had tried putting ^ in front and $ behind, but it making result come out as null.

i need help
  • 2,386
  • 10
  • 54
  • 72
  • 1
    Can you give an example of what you are actually trying to do? Are you trying to find all 5-digit numbers in a string? – Wolph Feb 12 '11 at 01:16
  • I'm reading a full html into variable. From the variable, want to grab out all numbers with the pattern of exactly 5 digits. – i need help Feb 12 '11 at 01:45

5 Answers5

85

I am reading a text file and want to use regex below to pull out numbers with exactly 5 digit, ignoring alphabets.

Try this...

var str = 'f 34 545 323 12345 54321 123456',
    matches = str.match(/\b\d{5}\b/g);

console.log(matches); // ["12345", "54321"]

jsFiddle.

The word boundary \b is your friend here.

Update

My regex will get a number like this 12345, but not like a12345. The other answers provide great regexes if you require the latter.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
10

My test string for the following:

testing='12345,abc,123,54321,ab15234,123456,52341';

If I understand your question, you'd want ["12345", "54321", "15234", "52341"].

If JS engines supported regexp lookbehinds, you could do:

testing.match(/(?<!\d)\d{5}(?!\d)/g)

Since it doesn't currently, you could:

testing.match(/(?:^|\D)(\d{5})(?!\d)/g)

and remove the leading non-digit from appropriate results, or:

pentadigit=/(?:^|\D)(\d{5})(?!\d)/g;
result = [];
while (( match = pentadigit.exec(testing) )) {
    result.push(match[1]);
}

Note that for IE, it seems you need to use a RegExp stored in a variable rather than a literal regexp in the while loop, otherwise you'll get an infinite loop.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
outis
  • 75,655
  • 22
  • 151
  • 221
2

This should work:

<script type="text/javascript">
var testing='this is d23553 test 32533\n31203 not 333';
var r = new RegExp(/(?:^|[^\d])(\d{5})(?:$|[^\d])/mg);
var matches = [];
while ((match = r.exec(testing))) matches.push(match[1]);
alert('Found: '+matches.join(', '));
</script>
Mark Eirich
  • 10,016
  • 2
  • 25
  • 27
  • Although it isn't clear whether he wants to match the 23553 in d23553 or not... Vague questions are so annoying! – Mark Eirich Feb 12 '11 at 01:41
  • .. and if the OP *does* want to match `d23553`, [my regex](http://stackoverflow.com/questions/4975644/regular-expression-to-match-exactly-5-digits/4975676#4975676) is quite inadequate! :P – alex Feb 12 '11 at 01:43
2

what is about this? \D(\d{5})\D

This will do on:

f 23 23453 234 2344 2534 hallo33333 "50000"

23453, 33333 50000

Luke
  • 8,235
  • 3
  • 22
  • 36
2

No need to care of whether before/after this digit having other type of words

To just match the pattern of 5 digits number anywhere in the string, no matter it is separated by space or not, use this regular expression (?<!\d)\d{5}(?!\d).

Sample JavaScript codes:

var regexp = new RegExp(/(?<!\d)\d{5}(?!\d)/g); 
    var matches = yourstring.match(regexp);
    if (matches && matches.length > 0) {
        for (var i = 0, len = matches.length; i < len; i++) {
            // ... ydo something with matches[i] ...
        } 
    }

Here's some quick results.

  • abc12345xyz (✓)

  • 12345abcd (✓)

  • abcd12345 (✓)

  • 0000aaaa2 (✖)

  • a1234a5 (✖)

  • 12345 (✓)

  • <space>12345<space>12345 (✓✓)

Muhammad Saqib
  • 2,185
  • 3
  • 35
  • 48