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.
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
match(/(\d{5})/g);
Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With