簡體   English   中英

Javascript正則表達“無效組”?

[英]Javascript regex “Invalid group”?

我正在嘗試使用javascript用戶名驗證正則表達式

' ^(?=.{4,16}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
'  └─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
'        │         │         │            │           no _ or . at the end
'        │         │         │            │
'        │         │         │            allowed characters
'        │         │         │
'        │         │         no __ or _. or ._ or .. inside
'        │         │
'        │         no _ or . at the beginning
'        │
'        username is 4-16 characters long

當我在Titanium Appcelerator上使用它時,我遇到了這個錯誤

[ERROR] :  Error generating AST for "***register.js"
[ERROR] :  Invalid regular expression: /^(?=.{4,16}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/: Invalid group
[ERROR] :  Alloy compiler failed

我的代碼:

var regex = /^(?=.{4,16}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/;

        if ( !regex.test(e.value)) 
         {
            inputs.Username.borderColor = 'red';
            inputs.Username.backgroundColor = '#edcaca';
            return false;
         }

任何想法為什么它給出錯誤無效組?

這可能對你有用:

/^(?=.{4,16}$)(?![_.])(?!.*[_.]{2})[a-z0-9._]+[a-z0-9]$/i

或者避免大多數前瞻:

/^(?!.*[_.]{2})[a-z0-9][a-z0-9._]{2,14}[a-z0-9]$/i

在線嘗試

問題是JavaScript不支持lookbehinds(正(?<=...)和負(?<!...) )。

JavaScript的正則表達式引擎不支持lookbehind : (?<![_.])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM