In my Javascript angular application I have a regex to validate usernames.
The issue I am facing, after doing much research is that utilising negative lookahead with expressions that contain white spaces is not working.
The Requirements
Username can be composed of many alphanumeric strings split at most with one space. Spaces at edges are not allowed. also the username should be filtered against a couple of banned names.
1)
(/^[a-zA-Z\d]+([\s][a-zA-Z\d]+)+?$/).test("admin may not be used")
allows alphanumeric words to be split by one consequent space at a time, and disallows spaces at edges
2)
(/^(?!(?:admin|alfred)$)[a-zA-Z\d]+$/).test("admin")
works and word admin is not allowed
3) merging both:
(/^(?!(?:admin|alfred)$)[a-zA-Z\d]+([\s][a-zA-Z\d]+)+?$/).test("admin may not be used")
fails! and will allow the banned word admin to be used.
Expected Result:
Both filters are expected to work, that is the banned words list , as well as consequent space filter. Can you please point what possibly is wrong with my expression?