2

How can I extract the string "XMLFileName" from the below URL using regular expression

var x = "C:\Documents and Settings\Dig\Desktop\XMLFileName.xml"

Thanks

0

2 Answers 2

4

You could do it with split(), pop() and replace()...

var filename = x.split('\\').pop().replace(/\..+$/, '');

jsFiddle.

You could also use a single regex...

var filename = x.replace(/.*\\|\..*$/g, '');

jsFiddle.

Ensure you escape the \ in your string literal too.

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

4 Comments

that will give you the extension as well.
Working like a charm :-) Very thanks. I am very poor in regular expressions that is why I have asked for help.
@32bitkid: Didn't notice that requirement, I'll make an edit.
@32bitkid I have expected an answer from you when I see a comment "oh common! really?" on Question :-)
0

You can use: "[^\\]*$"

but why not using regular javascript functions like indexOf() etc.

2 Comments

"[^\\]$" will match the very last character of the extension. He appears to not want the extension, and he does want more than one letter.
for the first problem you are correct it was a typo, and I fixed it, as for the second my bad I thought he needed the filename

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.