Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Regex - Cannot start/end with punctuation, no repeating punctuation

I'm trying to create a Regex. The rules are:

1) Only letters (both upper and lower), numbers, periods (.) and underscores (_)

2) Must begin and end with letter or number (i.e. no periods/underscores)

3) Cannot have multiple punctuations in a row, of any kind (no .. or ._ or _. or __ or .._. etc)

4) Must contain at least 2 letters (either upper or lower or both, doesn't matter)

5) No less than 3 chars, no more than 25 chars

So far, what I have is:

^((?!.*[\._]{2})[A-Za-z0-9\._]{3,25})$

Which is close-ish. I've got rules 1, 3 and 5, but cannot figure out 2 and 4.

I thought that

(([A-Za-z0-9]?)([A-Za-z0-9\._]+)([A-Za-z0-9]?)){3,25}

would get rule 2, but it just breaks it.

I've been working on this for a couple hours, but none of the solutions I can find, or can come up with, are quite what I'm looking for.

An explanation of the Regex statement you provide would be greatly appreciated, but is not entirely necessary.

EDIT 1

As per noob's suggestion:

^((?!.*[\._]{2})(([\w\d\.]+)){3,25})$

EDIT 2

Valid:

1rockit.man
q3w
e4.45r.d.2
sp_am

(and anything else between 3 and 25 characters, without leading or repeating punctuation)

Invalid:

.ret.
123
12a
ret..wer
super_.duper
_petrat
petrat_
like image 338
Birrel Avatar asked May 16 '26 01:05

Birrel


2 Answers

Based upon your conditions I came up with following regex. It's only modification over your current regex.

Regex: ^(?=.*[A-Za-z].*[A-Za-z])([A-Za-z0-9]((?!.*[\._]{2})([\w\d\.]{1,23}))[A-Za-z0-9])$

Explanation:

  • The first part (?=.*[A-Za-z].*[A-Za-z]) checks for whole string to have two characters separated by anything (that's pretty much the case here).

  • I added checks on the beginning and end of string which should be either letter or number.

  • Apart from that the length will also vary now as first and last character is counted rest of the length will be only 23 characters.

Regex101 Demo

Building the regex from the rules (in an order of convenience):

  • both upper and lower case => use case insensitive flag //i
  • letters, numbers, underscore and period => [\w.]*
  • 3-25 characters => ^[\w.]{3,25}$
  • cannot begin or end with period or underscore => (?!^[\W_]|[W_]$)
    (notice uppercase \W meaning not \w)
  • cannot contain 2 punctuation in a row => (?![\W_]{2})
  • checking the negative lookahead in every position => (?:(?!...)[\w.]){3,25}
    (using non-capturing group (?:) instead of () because we don't need to save the group)
  • at least 2 letters => (?:.*[a-z]){2} assuming i flag
  • wrap into lookahead (not consuming any characters) so we can check multiple conditions => (?=(?:...))

The final regex literal:

/^(?=(?:.*[a-z]){2})(?:(?!^[\W_]|[\W_]{2}|[\W_]$)[\w.]){3,25}$/i

https://regex101.com/r/yK1cB3/5#javascript


FTR: in languages that support the x flag (ignoring whitespace inside the regex), you could make it more readable (see a workaround for javascript):
^
(?=
  (?:.*[a-z]){2}
)
(?:
   (?!^[\W_]|[\W_]{2}|[\W_]$)
   [\w.]
){3,25}
$
like image 40
Aprillion Avatar answered May 17 '26 13:05

Aprillion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!