Skip to content Skip to sidebar Skip to footer

Extract File Name From A Path Using Regular Expression

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

Solution 1:

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.

Solution 2:

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

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

Post a Comment for "Extract File Name From A Path Using Regular Expression"