What does $1, $2, etc. mean in Regular Expressions?

后端 未结 2 2083
一个人的身影
一个人的身影 2020-12-25 11:10

Time and time again I see $1 and $2 being used in code. What does it mean? Can you please include examples?

相关标签:
2条回答
  • 2020-12-25 11:29

    When you create a regular expression you have the option of capturing portions of the match and saving them as placeholders. They are numbered starting at $1.

    For instance:

    /A(\d+)B(\d+)C/
    

    This will capture from A90B3C the values 90 and 3. If you need to group things but don't want to capture them, use the (?:...) version instead of (...).

    The numbers start from left to right in the order the brackets are open. That means:

    /A((\d+)B)(\d+)C/
    

    Matching against the same string will capture 90B, 90 and 3.

    0 讨论(0)
  • 2020-12-25 11:32

    This is esp. useful for Replacement String Syntax (i.e. Format Strings) Goes good for Cases/Case Foldings for Find & Replaces. To reference a capture, use $n where n is the capture register number. Using $0 means the entire match. Example : Find: (<a.*?>)(.*?)(</a>) Replace: $1\u$2\e$3

    0 讨论(0)
提交回复
热议问题