简体   繁体   English

用相应的字母数字替换文件名的最后一个字母

[英]Replace the last letter of a file name with the corresponding alphabet number

I am working on a renamer application which is doing various stuff using Javascript .我正在开发一个使用Javascript做各种事情的重命名应用程序 Application gives me this function by default function(index, item) { } and I have to write my code in there, which could looks like this function(index, item) {return item.newBasename...etc} .应用程序默认给我这个函数function(index, item) { } ,我必须在那里写我的代码,它看起来像这个function(index, item) {return item.newBasename...etc}

What I want to do is to replace the last letter of a filename , to its corresponding alphabet number .我想要做的是将文件名最后一个字母替换为其相应的字母编号 For example from "Filename AA " to "Filename A1 ".例如从“文件名AA ”到“文件名A1 ”。

Based on this example below, how can I do this?基于下面的这个例子,我该怎么做?

function(index, item) {
    return item.newBasename.replace(???);
}

You can use String.prototype.replace with a regular expression to match the last letter of the filename, and a function to find the corresponding letter position in the alphabet.您可以将String.prototype.replace与正则表达式一起使用以匹配文件名的最后一个字母,并使用一个函数来查找字母表中相应的字母位置。

For example:例如:

 const basename = 'hello-world'.replace(/([az])$/i, l => 'abcdefghijklmnopqrstuvwxyz'.indexOf(l.toLowerCase()) + 1); console.log(basename);

I finally found the solution...我终于找到了解决方案...

function(index, item) {
    str=item.newBasename;
    num=str.toUpperCase().slice(-1).charCodeAt(0) - 64;
    if (num>0 && num < 27) str = str.slice(0, str.length - 1) + num;
    return str; 
}

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

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