Can someone help me create this regex. I need it to check to see if the string is either entirely whitespace(empty) or if it only contains positive whole numbers. If anything else it fails. This is what I have so far.
/^\s*|[0-9][0-9]*/
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re looking for:
If you want a positive number without leading zeros, use
[1-9][0-9]*If you don’t care about whitespaces around the number, you can also try:
Note that you don’t want to allow partial matching, for example
123abc, so you need the start and end anchors:^...$.Your regex has a common mistake:
^\s*|\d+$, for example, does not enforce a whole match, as is it the same as(^\s*)|(\d+$), reading, Spaces at the start, or digits at the end.