I need regex to check if a string contains 8 decimal digits or more. It can contain anything else and the digits don't have to be consecutive.
Thanks in advance
EDIT: replaced "number" by "decimal digit" to match accepted answer.
A better regex would be /^\d*\.?\ d+$/ which would force a digit after a decimal point. @Chandranshu and it matches an empty string, which your change would also solve.
\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ). \s (space) matches any single whitespace (same as [ \t\n\r\f] , blank, tab, newline, carriage-return and form-feed).
match(/(\d{5})/g);
* - means "0 or more instances of the preceding regex token"
/([^\d]*\d){8}/
Perhaps not the most elegant / efficient way to do it, but it works. Basically it will match eight decimals (optionally, with non-decimals between them). If there are more than eight, it will match too.
EDIT
As @Tomalak has pointed out, [^\d]
equals \D
by definition:
/(\D*\d){8}/
A tweak to allow the last character to be non-numeric:
/(\D*\d){8,}\D*/
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