Javascript 正则表达式匹配 URL 末尾的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8045122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:30:16  来源:igfitidea点击:

Regex to match filename at end of URL

javascriptregex

提问by Juan

Having this text:

有这样的文字:

http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg

And other texts like this where the last 1can be any other number and the last 44can be any other number as well, I need a regex that will match /1t44.jpg.

和其他类似的文本,其中最后一个1可以是任何其他数字,最后一个44也可以是任何其他数字,我需要一个匹配/1t44.jpg.

Everything I've tried so far (/.+?\.([^\.]+)$) matches from the first slash (//img.oo.com.au/prod/CRWWBGFWG/1t44.jpg).

到目前为止我尝试过的所有内容 ( /.+?\.([^\.]+)$) 都与第一个斜杠 ( //img.oo.com.au/prod/CRWWBGFWG/1t44.jpg)匹配。

I'm using JavaScript, so whatever works on RegexPalshould do.

我正在使用 JavaScript,所以无论在RegexPal工作都应该做。

回答by Scott Rippey

Here's a simple Regex that will match everything after the last /:

这是一个简单的正则表达式,它将匹配 last 之后的所有内容/

/[^/]*$

回答by jfriend00

If you want to match a filename with a very specific file extenstion, you can use something like this:

如果要将文件名与非常具体的文件扩展名匹配,可以使用以下内容:

/\/\dt\d\d\.jpg$/

This matches:

这匹配:

  • a slash
  • followed by a digit
  • followed by the letter 't'
  • followed by two digits
  • followed by '.jpg' at the end of the string
  • 斜线
  • 后跟一个数字
  • 后跟字母“t”
  • 后跟两位数字
  • 后跟字符串末尾的“.jpg”

Or, if you really just want the filename (whatever is after the last slash with any file extension), then you can use this:

或者,如果您真的只想要文件名(带有任何文件扩展名的最后一个斜杠之后的任何内容),那么您可以使用:

/\/[^\/]+$/

This matches:

这匹配:

  • a slash
  • followed by one or more non-slash characters
  • at the end of the string
  • 斜线
  • 后跟一个或多个非斜杠字符
  • 在字符串的末尾

In your sample string of http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg, both of these will match /1t44.jpg. The first is obviously much more restrictive since it requires a specific format of the filename. The second matches any filename.

在您的示例字符串中http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg,这两个都将匹配/1t44.jpg。第一个显然更具限制性,因为它需要文件名的特定格式。第二个匹配任何文件名。



Other choices. In node.js development, you can use the pathmoduleand use path.parse()to break a path up into all of its various components.

其他选择。在 node.js 开发中,您可以使用path模块和用于path.parse()将路径分解为所有不同的组件。

And, there are various libraries written for the browser that will break up a path into its components too.

而且,有为浏览器编写的各种库也可以将路径分解为其组件。

回答by Hemlock

As Johnsyweb says, a regular express isn't really needed here. AFAIK the fastest way to do this is with lastIndexOfand substr.

正如 Johnsyweb 所说,这里真的不需要普通快递。AFAIK 最快的方法是使用lastIndexOfsubstr

str.substr(str.lastIndexOf('/'));

回答by Johnsyweb

Of course you don't have to use a regular expression to splita stringand popthe last part:

当然,您不必split字符串pop最后一部分使用正则表达式:

var str="http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg";
var parts = str.split("/");
document.write(parts.pop() + "<br />");

回答by mycowan

This is for Java on a Linux machine. It grabs the last part of a file path, so that it can be used for making a file lock.

这适用于 Linux 机器上的 Java。它抓取文件路径的最后一部分,以便它可以用于制作文件锁。

// String to be scanned to find the pattern.
String pattern = ".*?([^/.]+)*$";

// Create a Pattern object
Pattern r = Pattern.compile(pattern);

// Now create matcher object.
Matcher match = r.matcher("/usr/local/java/bin/keystore");

/**
* Now you have two matches
* #0    /usr/local/java/bin/keystore
* #1    keystore
*/

String fileLock = "";

if (match.find()) {
    fileLock = match.group(1) + ".lock";
}

A little different than the original question, I know. But I hope this helps others who were stuck with the same problem I had.

我知道,与原始问题略有不同。但我希望这能帮助那些被我遇到同样问题的人。

回答by Liam Ray

Based on answer of Scott, try this: (JavaScript)

根据 Scott 的回答,试试这个:(JavaScript)

var url = "http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg";

var path           = url.replace(/(.*)([\\/][^\\/]*$)/, "" );

var lastElement    = url.replace(/(.*)([\\/][^\\/]*$)/, "" );

This can be also matched for Windows/Nix file path, to extract file name or file path :

这也可以匹配 Windows/Nix 文件路径,以提取文件名或文件路径:

c:\Program Files\test.js => c:\Program Files

c:\Program Files\test.js => \test.js