Search code examples
javascriptregexregex-lookarounds

Finding last occurrence of a pattern with lookaround


I have the path ../dir1/dir2/dir3/filename.txt and want to extract just filename between the final \ and . using regex. The file extension will also not always be .txt.

Currently I am doing (?<=\/).+(?=\.) but selects from the first \, including the directory names. I would love this to just be match-based and not using groups. Oh and using ECMA regex if that's important.


Solution

  • You can use the below regex to achieve your purpose:

    ([\w-]+)\..*$
    

    Explanation of the above regex:

    • ([\w-]+) - Capturing group that matches one or more (+) word characters (\w) or hyphens (-). Usually filenames contains \w characters so I used that but if it contains other special characters feel free to amend it as per your need.
    • \. - matches a literal dot character.
    • .* - matches any character (.) zero or more times (*).
    • $ - matches end of line. So basically the full regex matches any string that contains a word (consisting of one or more word characters or hyphens) followed by a dot and any characters until the end of the line. The word before the dot is captured for later use and this capturing group gives you your desired filename.

    enter image description here

    const regex = /.*?([\w-]+)\..*$/gm;
    
    const str = `../dir1/dir2/dir3/filename1.tar.gz
    ../dir1/dir2/dir3/filename2.tar`;
    const subst = `$1`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log(result);

    REGEX DEMO

    Alternative Way:(Using Javascript functions)

    let paths = `../dir1/dir2/dir3/filename1.tar.gz
    ../dir1/dir2/dir3/filename2.tar`;
    
    paths.split('\n').forEach(path => console.log(path.  
    split('/').pop().split('.').shift()));
    
    /* 
    * split('/').pop() - splits the path into an array using the forward slash (/) as the separator
    and removes and returns the last element of the array.  
    
    * split('.').shift() - splits the filename into an array using the dot (.) as the separator, 
    and removes and returns the first element of the array.
    * NOTE: paths.split('\n') might not be your exact requirement. I used it to showcase.
    */