I have the javascript regex /^[a-zA-Z]{6}[a-zA-Z0-9]{5}$/
but I would also like the test to pass if the string is less than or greater than 11 characters. So any string from 0-10 characters would pass, any string 12 or greater characters would pass, but if it is exactly 11 characters it will be evaluated by the previously mentioned test.
Asked
Active
Viewed 404 times
2

JarrettD5309
- 61
- 7
-
with pass do you mean to return True, right? I dont understand your question, in your regex you have the {6} characters for the first part and {5} for the second, 6+5=11 but you said 11 is exactly what you DONT want the lenght to be... – Gonzalo F S May 25 '21 at 19:19
-
If you are talking about **another** test that is not the regex please share the code :) – Gonzalo F S May 25 '21 at 19:20
-
@WiktorStribiżew Not removed, just evaluated. So for instance. '12@' would pass because it is less than 11. '1234$abcd!56789*efg' would pass because it is greater than 11. '1234567890a' would not pass because it is 11 characters but does not pass the previously mentioned regex if tested. 'aaaasrbb123' would pass because it is 11 characters and passes the previously mentioned regex if tested. – JarrettD5309 May 25 '21 at 19:21
-
I think I got it, so I removed the comment and posted the answer. – Wiktor Stribiżew May 25 '21 at 19:23
-
@GonzaloFS I would like a regex that returns true with the previously mentioned regex when the string being tested is 11 characters. I would also like this new regex to return true if tested with a string of less than 11 characters or greater than 11 characters. – JarrettD5309 May 25 '21 at 19:24
-
1The best way to communicate this info is to show us examples... examples that fail and examples that pass. Make sure you have at least one example for each bucket that you want to pass/fail, etc. It seems like <11, =11, and >11 would be at least three buckets. – JeffC May 25 '21 at 19:29
1 Answers
5
You can use
/^(?:.{0,10}|.{12,}|[a-zA-Z]{6}[a-zA-Z0-9]{5})$/
See the regex demo.
Details
^
- start of string(?:
.{0,10}
- zero to ten chars other than line break chars|
- or.{12,}
- twelve or more chars other than line break chars|
- or[a-zA-Z]{6}[a-zA-Z0-9]{5}
- six letters and then five alphanumeric chars
)
- end of the group$
- end of string.
To also match line break chars, you may add the /s
flag (if you are using the ECMAScript 2018 compliant JavaScript environment) or replace each .
with [\w\W]
/ [\s\S]
/ [\d\D]
/ [^]
.

Wiktor Stribiżew
- 607,720
- 39
- 448
- 563
-
1
-
1@RajdeepDebnath A [non-capturing group](https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions). – Wiktor Stribiżew May 25 '21 at 19:30
-
1