SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression question

    I'm reading a chapter in a book about regular expressions, and the author has written one which should match a particular text. There's only part of it that I have a quarrel with however. That part is looking for [sometexthere].

    And the expression he wrote goes like this:

    /\[([^\]]+)]

    Okay, so first he escapes the square bracket - /\[ so he'll find that.

    Then he uses the round brackets (()) to group the text in the middle, so that later it can be referred to as $1.

    Then he uses [^\]]+ to state that he wishes for there to be text that isn't a squared closing bracket, and then he simply adds the closing bracket. Why doesn't he escape the last bracket?

  2. #2
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, this answered itself:

    The square bracket doesn’t need to be escaped with a backslash because there is no unescaped opening square bracket.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •