[英]regex not detecting space in username textbox
我有一個asp:textbox,其用戶名是新用戶帳戶的注冊表單的一部分。
顯然,我不希望用戶使用空格作為名稱進行注冊,因此我擁有此正則表達式,該表達式應將有效輸入的ASCII字符的長度保持在3到16之間,並且沒有空格。
但實際上沒有空格是行不通的。 它適用於Regex編輯器和檢查器,但不適用於我的aspx頁面。
有什么建議么?
^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$|\s
非常感謝
^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`?/{}]{3,16})$|\s ↑
您當前的正則表達式說: “如果字符串由這些字符組成並且長度為3至16個字符, 或者如果包含空格字符,則匹配字符串。”
因此,如果您不希望它與空格匹配,請從正則表達式中除去|\\s
(即'or'運算符和空白模式)。
您似乎無法理解dtb想要說的話。 讓我為您分解正則表達式,您將明白他在說什么:
^ - matches the beginning of the input string ( - begins a capture group, in your case useless and can be removed along with the closing ) just before the $ [ - begins a group of characters a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`?/{} - defines all the characters allowed, NOTICE there is no space character so spaces will not count ] - ends the group of characters {3,16} - says that the preceding character(or group of characters in this case) must occur between 3 and 16 times ) - closes the capture group, again can be removed with the open ( $ - matches the end of the input string
這是您的表情出現問題的地方...
| - says that the preceeding match expression (this is the $ which is the end of input) OR the following must be true, but not necessarily both \s - matches a space or tab anywhere in the input string
所以(如果我沒看錯的話)您的正則表達式指出:
“如果字符串以ascii字符開頭並且長度為3到16個字符,那么它會在找到字符串的末尾或某些空格(制表符或空格)之前與之匹配。”
要解決此問題,請從表達式末尾刪除“ | \\ s”,然后使用以下命令:
^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$
一個簡單的答案是:
^([^\s]{3,16})$
這表示“整個字符串必須包含3到16個除空白以外的任何重復。”
這也將允許帶重音的字符,但是無論如何,這可能是您需要接受的。
更簡單的是
^\S{3,16}$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.