0

I have a pattern as follows:

document_data/Filename.xyz

I'm trying to write a regular expression that will only return "Filename". I have one that gives me just "xyz" and it works, and I have one that gives me "Filename.xyz" but I"m having trouble chopping off the xyz to just get Filename.

[^/]+$ gives me "Filename.xyz" \[^.]+$ gives me xyz

I'm not a regex person so any help would be great! Thanks!

1

3 Answers 3

1

try to use a lookahead (?=...):

/[^\/.]+(?=\.[^.]+$)/

A lookahead performs only a check and means followed by but match nothing.

Sign up to request clarification or add additional context in comments.

1 Comment

one of my co-workers came up with this one and it seems to work. It all seems so simple now! /[a-zA-Z0-9]+ Thanks to all for the help!
0

Combine the two and wrap the first part in a capturing group:

"document_data/Filename.xyz".match(/([^/]+)\.[^.]+$/)[1]

Comments

0

I hope its could help (cf: http://planetozh.com/blog/2008/04/javascript-basename-and-dirname/)

    function basename(path) {
        return path.replace(/\\/g,'/').replace( /.*\//, '' ).replace( /\..*/, '' );
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.