SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple regex question

    HI,

    I have this regex:

    Code JavaScript:
     var src = button.src.replace(/(\.[^.]+)$/, '_over$1');


    Now i know what it does and how some of it works....ive done a bit of regex in php and its almost the same in javascript i think. Basically its finding the period(.) in the file and adding, before the period, the letters _over. Its then putting the file extension back in. If someone could spell the regex out for me that would be great! I think its saying replace a 1 or more dots with this.....

    so file.gif becomes file_over.gif.

    What i dont quite understand is how the file extension is put back in. Im guessing that the $1 at the end stands for the bit after the dot ( the extension) If it does then suppose $0 would give me the start of the filename ( before the dot).

    could anyone clarify?

  2. #2
    SitePoint Member
    Join Date
    Dec 2004
    Location
    New Philadelphia
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. The '/' that you see at the beginning and end is the regex delimiter. Hence, if you need to capture a '/' you will need to escape it.

    2. There are 2 special characters that identify the beginning of a regex and the end of the regex. They are '^' and '$' respectively. You will see the '$' which only matches the end of the match'

    3. The '(\.[^.]+)' is the meat of the work. The parenthesis is a capture reference and the value captured within this match is available as $1. If you had another one, it would be available as $2 and so on. Notice the '\' before the '.'. In regex a period has special meaning, it matches any character. However, here we are only looking for a literal period so it must be escaped with the '\'. So - we are looking for a literal period. Next we are trying to match characters with '[]' Inside of this matching, the '^' has a different meaning that if it were on the outside. It simply negates the characters you have specified in the brackets. So, we are matching a literal period, then any characters that are NOT a period. The '+' indicates that we need to have at least one, but could have many.

    4. The match that is found is then stored in $1 as we have stated before. The RegExp object uses its replace method to replace any instances of the matches with '_over' plus the string that is captured through the match.

    I hope this helps a little bit....

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi natekaiber, Thanks for the excellent reply, its really helped me alot

  4. #4
    SitePoint Enthusiast Bellthorpe's Avatar
    Join Date
    Aug 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll find a useful Regex tutorial here.
    Last edited by Bellthorpe; Jul 9, 2007 at 22:20.
    Don't pay for webhosting features you don't need.
    Build your own webhosting plan.
    1,238 Unicode fonts for 75 written language groups.


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
  •