javascript - Extract file name from a path using regular expression - Stack Overflow

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

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

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

Share Improve this question asked Dec 8, 2011 at 5:27 ExceptionException 8,38924 gold badges88 silver badges141 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 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.

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895490a4599670.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信