[英]RegExp to validate the string “900 - 09 999”
驗證以下字符串“ 900-09 999”的正確Javascript RegExp是什么?該字符串應僅允許使用0到9之間的數字,連字符和空格。
提前致謝
在解釋您要讓正則表達式驗證的內容時,請始終保持精確。 空格和連字符是否可選? 這東西很重要。 無論如何,這驗證了嚴格的格式:
"\d{3} \- \d{2} \d{3}"
這是一個不太嚴格的:
"\d{3} ?\-? ?\d{2} ?\d{3}"
如果使用此:
inputField.value = inputField.value.replace(/\s*(\d\d\d)\s*-?\s*(\d\d)\s*(\d\d\d)\s*/, "$1 - $2 $3")
...如果不完全匹配,則應同時松散地驗證和重新格式化該值。
細分后,該表達式將執行以下操作:
\s* # match any amount of whitespace
(\d\d\d) # capture three digits
\s* # match any amount of whitespace
-? # match an optional hyphen
\s* # match any amount of whitespace
(\d\d) # capture two digits
\s* # match any amount of whitespace
(\d\d\d) # capture three digits
\s* # match any amount of whitespace
match
表示查找與表達式匹配的一組字符 capture
意味着找到一個匹配項,但存儲該匹配項以供以后使用 whitespace
可以是空格,制表符或回車符 any amount
都可以表示零或更多
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.