Reputation: 1653
I need to build a regex that doesn't match the words with this requirements:
a-z0-9_-
..
ok, ..
nopethis is what i did:
/[0-9a-zA-Z\-\_\.]{3,32}/
the problem is that i can insert more than one .
and i don't know how to fix it.
Upvotes: 1
Views: 58
Reputation: 241278
You could use the following expression:
/(?:[\w-]|\.(?!\.)){3,32}/
Explanation:
(?:
- Start of a non-capturing group[\w-]
- Character set to match [a-zA-Z0-9_-]
|
- Alternation, or..\.(?!\.)
- Negative lookahead to match a .
character literally if it isn't followed by another .
character.)
- Close the non-capturing group{3,32}
- Match the group 3 to 32 timesYou may also want to add anchors if you want to match the entire string against the expression:
/^(?:[\w-]|\.(?!\.)){3,32}$/
Upvotes: 3