I need to find a regex that tests that an input string contains exactly 10 numeric characters, while still allowing other characters in the string.
I'll be stripping all of the non-numeric characters in post processing, but I need the regex for client-side validation.
For example, these should all match:
But these should not:
This seems like it should be very simple, but I just can't figure it out.
To check if a string contains only numbers in JavaScript, call the test() method on this regular expression: ^\d+$ . The test() method will return true if the string contains only numbers. Otherwise, it will return false . The RegExp test() method searches for a match between a regular expression and a string.
You can use out\dmf\d+ , or, if you want to match only 1 or 2 digits at the end, out\dmf\d{1,2} .
Python Regex – Get List of all Numbers from String. To get the list of all numbers in a String, use the regular expression '[0-9]+' with re. findall() method. [0-9] represents a regular expression to match a single digit in the string.
/^\D*(\d\D*){10}$/
Basically, match any number of non-digit characters, followed by a digit followed by any number of non-digit characters, exactly 10 times.
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