使用正则表达式从路径中提取文件名 [英] Extract file name from a path using regular expression

查看:1553
本文介绍了使用正则表达式从路径中提取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正则表达式从以下URL中提取字符串XMLFileName

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

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

谢谢

推荐答案

你可以用 split() pop来做到这一点() replace() ...

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.

这篇关于使用正则表达式从路径中提取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆