I need to build a JavaScript regular expression with the following constraints:
- The input string needs to be at least 6 characters long
- The input string needs to contain at least 1 alphabetical character
- The input string needs to contain at least 1 non-alphabetical character
I'm seriously lacking a lookback feature in JavaScript. The thing I came up with:
((([a-zA-Z][^a-zA-Z])|([^a-zA-Z][a-zA-Z]))....)|
(.(([a-zA-Z][^a-zA-Z])|([^a-zA-Z][a-zA-Z]))...)|
(..(([a-zA-Z][^a-zA-Z])|([^a-zA-Z][a-zA-Z]))..)|
(...(([a-zA-Z][^a-zA-Z])|([^a-zA-Z][a-zA-Z])).)|
(....(([a-zA-Z][^a-zA-Z])|([^a-zA-Z][a-zA-Z])))
This looks pretty long. Is there a better way?
How I came to this:
- Regex for alphabetical character is
[a-zA-Z] - Regex for non-alphabetical character is
[^a-zA-Z] - So I need to look for a
[a-zA-Z][^a-zA-Z]or[^a-zA-Z][a-zA-Z]
so(([a-zA-Z][^a-zA-Z])|([^a-zA-Z][a-zA-Z])). - I need to check for n preceding characters and 6-n succeeding characters.