简体   繁体   English

如何可靠地获取不带JavaScript后缀的文件名?

[英]How to reliably get the filename without the suffix in javascript?

Whats the reliable way on getting the filename of a file by removing suffix in javascript? 通过删除javascript中的后缀来获取文件名的可靠方法是什么?

I have a file let's say : 我有一个文件,可以说:

http://example.com/uploads/images/a51dd42_thumb.jpg and http://example.com/uploads/images/a51dd42_s.jpg http://example.com/uploads/images/a51dd42_thumb.jpghttp://example.com/uploads/images/a51dd42_s.jpg

Now i want to get the string http://example.com/uploads/images/a51dd42.jpg 现在我想获取字符串http://example.com/uploads/images/a51dd42.jpg

replace() function is not what i want since images filename could have many different types of suffixes replace()函数不是我想要的,因为图像文件名可能具有许多不同类型的后缀

Maybe regex is the best for this? 也许正则表达式是最好的?

If yes, whats the reliable code for it? 如果是,那么可靠的代码是什么?

Thanks in advance 提前致谢

The replace function probably is what you want, and it can accept a regular expression as the search pattern: replace函数可能就是您想要的,它可以接受正则表达式作为搜索模式:

url = url.replace(/_[^\/]+(\.[^\/]*)$/, "$1");

What that expression does: Example on Regex101 该表达式的作用: Regex101上的示例

  • Looks for an _ that isn't followed by any slashes (so we're only looking at the final segment in the path). 寻找一个不带斜杠的_ (因此,我们只查看路径中的最后一段)。

  • Allows any number of non-slash characters after the _ . 允许在_之后使用任意数量的非斜杠字符。

  • Stops matching those at the last . 停止匹配最后一个. it finds followed by zero or more characters (I'm assuming these always have an extension on them); 它发现后跟零个或多个字符(我假设这些字符始终带有扩展名); captures the . 捕获. and the characters after it (eg, the extension). 以及其后的字符(例如扩展名)。

  • Replaces the overall match with just the . 用代替整体比赛. and extension following it. 以及后续的扩展。 (The $1 in the replace string is special, it means "use the value of the first capture group.) (替换字符串中的$1是特殊的,它表示“使用第一个捕获组的值。”

If your paths may or may not have an extension on them, just add a ? 如果您的路径可能带有或不带有扩展名,只需添加一个? near the end of the regex, just before the $ : /_[^\\/]+(\\.[^\\/]*)?$/ (that makes everything in the capture group optional). 在正则表达式末尾附近,就在$ :/ /_[^\\/]+(\\.[^\\/]*)?$/ ( /_[^\\/]+(\\.[^\\/]*)?$/ $ / /_[^\\/]+(\\.[^\\/]*)?$/ (这使得捕获组中的所有内容都是可选的)。

Example: Live Copy 示例: 实时复制

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Example</title>
  <style>
    body {
      font-family: monospace;
    }
  </style>
</head>
<body>
<script>
  (function() {
    "use strict";

    test("http://example.com/uploads/images/a51dd42_thumb.jpg");
    test("http://example.com/uploads/images/a51dd42_s.jpg");

    function test(url) {
      display("Before: " + url);
      url = url.replace(/_[^\/]+(\.[^\/]*)$/, "$1");
      display("After: " + url);
    }

    function display(msg) {
      var p = document.createElement('p');
      p.innerHTML = String(msg);
      document.body.appendChild(p);
    }
  })();
</script>
</body>
</html>

You can use: 您可以使用:

var s = 'http://example.com/uploads/images/a51dd42_thumb.jpg';
var r = s.replace(/^(.+?)_[^.]+(\.[^\/.]+)$/i, '$1$2');
//=> http://example.com/uploads/images/a51dd42.jpg

Split it easy 轻松拆分

var start = Filename.split('_')[0], 
file = Filename.split('_')[1],
end = file.split('.')[1];
console.log(start + '.' + end);

.

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

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