Extract file name from a path using regular expression

后端 未结 2 1047
小蘑菇
小蘑菇 2020-12-10 00:24

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

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


        
相关标签:
2条回答
  • 2020-12-10 00:44

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

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

    0 讨论(0)
  • 2020-12-10 00:54

    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.

    0 讨论(0)
提交回复
热议问题