Regex: parsing GitHub usernames (JavaScript)

I am trying to parse GitHub usernames (which start with @) from a paragraph of text to associate them with related profiles.

GitHub username restrictions:

  • Alphanumeric with single hyphens (without consecutive hyphens)
  • You cannot start or end a hyphen (if it ends with a hyphen, just compare everything until then)
  • Maximum length 39 characters.

For example, the following text:

Example @valid hello @ valid-username: @ another-valid-username, @ -invalid @ in-valid @ ignore-last-dash- an@email.com @ another-valid?

script ...

Should match:

  • @valid
  • @ valid-username
  • @ other-valid-username
  • @in
  • @ ignored last tier
  • @

:

  • @-
  • an@email.com

JavaScript, :

/\B@((?!.*(-){2,}.*)[a-z0-9][a-z0-9-]{0,38}[a-z0-9])/ig

(, @a).

: https://regex101.com/r/rZ5eW1/2

? ?

+4
3
/\B@([a-z0-9](?:-?[a-z0-9]){0,38})/gi

. , (.. ., --), @ . OP , , . , ( ):

@abc.123
@abc--123
@abc-

, . Regex101 , :

  • \B: " ", , , , , - someones.@email.com . , [1] @reference.

    Honore Doktorr , lookbehind JS.

  • @: @. , , .

  • (...): . , , , @, , - .
  • [a-z0-9]: . - i . , .
  • (?:...): . , .
  • -?[a-z0-9]: - , . , . , - - -, -, [a-z0-9].
  • {0,38}: 0 38 , . # 4 39 . , , .
+3

.

/\B@(?!.*(-){2,}.*)[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?\b/ig

. :

  • (?!.*(-){2,}.*): , .
  • [a-z0-9]: @ - .
  • (?:[a-z0-9-]{0,37}[a-z0-9])?: 0-37 - , - , № 2 , , , . (?:…) .
  • \b: break ( -).
+2

RegEx, , github google ( ):

^@\w(-\w|\w\w|\w){0,19}$

:

  • ^:
  • @ -: .
  • \w: [A-Za-z0-9_], , ( )
  • $:
  • {0,19}: , .

:

  • RegEx ( ^ $)
  • @, ( ), (@A, @1 @_)
  • (...){0,19}:

    • \w (1- )
    • two \w (2nd opt)
    • \w ( )

    :

  • :

  • : @w-w
  • : , , , - .
  • 19 : 1- 2- 19*2=38 , 39 . , .

:

  • @ww-w...w ( 39 ).
  • @ww-w...w, 39 .

, ww-w w, , w ( 18 ), w- ( , 17, ), , 17 , 17*2=34. , 38 (34+2+1+1) , 39.

, , , RegEx . , javascript.

0

All Articles