正則表達式:檢查數字和空格

[英]Regexp: Check for digits and whitespaces


I am using the following regexp to make sure my string contains 5 digits. It works perfect, however, I also need to allow whitespaces: 123 45 and 12345 need to pass the test.

我使用以下正則表達式來確保我的字符串包含5位數。它工作得很完美,但是,我還需要允許空格:123 45和12345需要通過測試。

string.match(/^\d{5}$/g);

Thanks

謝謝

1 个解决方案

#1


3  

You could simply remove any whitespace and then try to match the rest:

你可以簡單地刪除任何空格,然后嘗試匹配其余的:

string.replace(/\s/g, "").match(/^\d{5}$/g)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/02/09/729e4b09c8980d126fa4e4d4d47ca14f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com