regular expression to match exactly 5 digits
Asked Answered
T

5

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.

Tigress answered 12/2, 2011 at 1:13 Comment(3)
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?Marigolde
I'm reading a full html into variable. From the variable, want to grab out all numbers with the pattern of exactly 5 digits.Tigress
The answer you selected is in conflict with your stated requirement, "No need to care of whether before/after this digit having other type of words" (the wording of which should be improved). You did not say that strings of five digits must be surrounded with word boundaries.Antipersonnel
U
86

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.

Unconscious answered 12/2, 2011 at 1:19 Comment(7)
@Joel Etherton Just tried it, and it didn't match it. I'm probably missing something, please elaborate for me :)Unconscious
@Unconscious - My comment was made when you had it at str.match(/\d{5}/g); and it did match it.Crystlecs
@Joel Etherton Yeah, it was a rushed answer and incorrect. Hopefully this new one is OK :)Unconscious
@Unconscious - +1 now that you have the word boundary in there. :)Crystlecs
This will not work for 34_545_323_12345_54321_123456 :( Can't figure this one outAppropriation
_ is a word character and not a boundary (by at least the regex definition), see /\w/.test('_').Unconscious
Word Boundary!! Marvelous - I've been doing regex for 20 years and never realized this was a thing. Always had to play games with wild cards. Feel like such a rube!Paperboy
A
11

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.

Annapolis answered 12/2, 2011 at 1:34 Comment(1)
Good looking results there, +1.Unconscious
B
3

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>
Bargain answered 12/2, 2011 at 1:33 Comment(3)
Although it isn't clear whether he wants to match the 23553 in d23553 or not... Vague questions are so annoying!Bargain
.. and if the OP does want to match d23553, my regex is quite inadequate! :PUnconscious
I expect this will be more efficient than using lookarounds.Antipersonnel
J
2

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

This will do on:

f 23 23453 234 2344 2534 hallo33333 "50000"

23453, 33333 50000

Janes answered 12/2, 2011 at 1:36 Comment(0)
L
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 (✓✓)

Leverett answered 15/7, 2018 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.