regex allow only numbers or empty string

后端 未结 6 539
失恋的感觉
失恋的感觉 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 04:41

    Kobi has a good answer but technically you don't have to capture it (unless you're going to do something the output)

    /^[\s\d]+$/
    

    Or if you don't care if the string is completely empty (i.e. "")

    /^[\s\d]*$/
    

    To clarify I understood the original question to mean whitespace in the string should be ignored.

提交回复
热议问题