I found this: Regex to match digits of specific length but it talks about Python. I am wanting to be able to get a group of random numbers of specific length. So if I have 167691#15316243
it will match 15316243
. I am not sure how to implement this. right now I have new RegExp('[0-9]+', "g");
which matches a group of numbers fine, but now I realized I will have some times when I have more than one group and I only want the group of eight numbers.
3 Answers
[0-9]+ - Matches one or more numbers
[0-9]{8} - Matches exactly 8 numbers.
[0-9]{8,10} - Matches between 8 and 10 numbers.
[0-9]{8,} - Matches 8 or more numbers.
[0-9]* - Matches zero or more numbers.
[0-9]? - Matches zero or one number.

- 4,017
- 2
- 25
- 20
-
[0-9]{8,} - minimum length is 8 – Tho Mar 29 '15 at 09:15
-
@ThoQLuong added, thanks! Was unable to accept the review due to reputation requirements. – Nathan Mar 30 '15 at 02:29
-
1Important note: do not add a space after coma in `{8,10}`. It should be exactly `{8,10}` not `{8, 10}` – Maksim Nesterenko Jun 14 '17 at 15:04
You can specify the length of a matching set using {}.
For example: [0-9]{8}
Which will match any numbers from 0 to 9 with a specific length of 8 characters.
You can also specify a min/max range instead of forcing a specific legnth. So if you wanted a min of 4 and a max of 8 the example would change to: [0-9]{4,8}

- 7,759
- 26
- 38
Simply put the repetition amount in curly braces:
"167691#15316243".match(/\d{8}/g);
Here's the fiddle: http://jsfiddle.net/3r5vd/
I'd suggest you read this article (scroll down to the section on Limiting Repetition).
Here's a quote:
Modern regex flavors [...] have an additional repetition operator that allows you to specify how many times a token can be repeated. The syntax is
{min,max}
, wheremin
is a positive integer number indicating the minimum number of matches, andmax
is an integer equal to or greater thanmin
indicating the maximum number of matches. If the comma is present butmax
is omitted, the maximum number of matches is infinite. So{0,}
is the same as*
, and{1,}
is the same as+
.
Omitting both the comma and max tells the engine to repeat the token exactlymin
times.

- 214,931
- 59
- 362
- 292