简体   繁体   中英

Javascript regex “Invalid group”?

i am trying to make javascript username verify regex

' ^(?=.{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

when i am using it on Titanium Appcelerator i got this error

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

my code :

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

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

any idea why its giving error invalid group ?

This might work for you:

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

Or avoiding most of the lookaheads:

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

Try it online

The problem is that lookbehinds (positive (?<=...) and negative (?<!...) ) are not supported by JavaScript.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM