regex allow only numbers or empty string

后端 未结 6 533
失恋的感觉
失恋的感觉 2021-02-05 04:30

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 el

6条回答
  •  抹茶落季
    2021-02-05 05:00

    To match a number or empty string '' i.e the user has not entered any input do this

    (^[0-9]+$|^$)
    

    To match a number, an empty string, or a white space character

    (^[0-9]+$|^$|^\s$)
    

    Test this on regex101

提交回复
热议问题