Search code examples
javascriptregexbackreference

Javascript regex backreference indexing


I need a regex to match a string that

  1. either starts with a special character like [#.>+~] and is followed by a lowercase ASCII word, or that
  2. consists only of a different special character like *.

The special character should be captured in group number 1, the following word (or, in the second case, the empty string) in group number 2.

I can handle the first case with /^([#\.>+~]?)([a-z]+)$/, but how can I get the second case into this regex to achieve the following results:

"#word"  -> 1 => "#", 2 => "word"
"~word"  -> 1 => "~", 2 => "word"
"##word" -> no match
"+#word" -> no match
"!word"  -> no match
"*word"  -> no match
"word"   -> 1 => "",  2 => "word"
"*"      -> 1 => "*", 2 => ""
"**"     -> no match
"*word"  -> no match

Solution

  • This regex should do what you need:

    /^([#~.>+](?=[a-z]+$)|[*](?=$))([a-z]*)$/
    

    See it on regex101.com

    Explanation:

    ^          # Start of string
    (          # Match and capture in group number 1:
     [#~.>+]   # Either: one "special character"
     (?=       #  but only if it's followed by
      [a-z]+   #   at least one lowercase ASCII letter
      $        #   and the end of the string.
     )         #  End of lookahead
    |          # OR
     [*]       #  one (different) special character
     (?=$)     #  but only if the string ends right after it.
    )          # End of the first capturing group
    (          # Match and capture in group number 2:
     [a-z]*    # Zero or more ASCII lowercase letters
    )          # End of the second capturing group
    $          # End of string