简体   繁体   English

如何从内容字符串获取文件路径和文件名?

[英]How to get a filepath and file name from a string of contents?

How can I get just get the file path and file name from a string? 如何仅从字符串获取文件路径和文件名?

My string looks like the following 我的字符串如下所示

GET /content/random.gif HTTP/1.0

I tried the regex ^(.+)/([^/]+)$ , but it didn't work. 我尝试了regex ^(.+)/([^/]+)$ ,但是没有用。 Is there an alternative to regex? 正则表达式有替代方法吗?

Just to extend the comments of using a string split. 只是为了扩展使用字符串拆分的注释。 To get the filepath and filename you could use the following: 要获取文件路径和文件名,可以使用以下命令:

 var str = 'GET /content/random.gif HTTP/1.0'; var file = str.split(' ')[1]; var filepath = file.substr( 0,file.lastIndexOf('/')+1 ); var filename = file.substr( file.lastIndexOf('/')+1 ); console.log('filepath: '+filepath); console.log('filename: '+filename); 

Short, simple and reliable 简短,简单,可靠

var str = 'GET /content/random.gif HTTP/1.0';
var re = /^.+? (.*)\/([^\/]*) .+?$/;
var m = str.match(re);

var filepath = m[1];
var filename = m[2];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM