Click here to Skip to main content
15,887,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using a renamer application which is doing various stuff using Javascript, I am trying to replace the last letter of some filenames, to the corresponding alphabet number. For example, from "Filename AA" to "Filename A1", from "Filename AB" to "Filename A2" etc.

What I have tried:

This application gives me this function by default...
JavaScript
function(index, item) { }
And I have to write my code in there, which could looks like this...
JavaScript
function(index, item) {return item.newBasename...etc}.
Based on this example below, how can I do this?
JavaScript
function(index, item) {
    return item.newBasename.replace(???);
}
Posted
Updated 20-Oct-18 23:41pm
Comments
Patrice T 20-Oct-18 8:56am    
RegEx looks like the complicated way to me.
you should try 'string functions'
Afzaal Ahmad Zeeshan 20-Oct-18 10:12am    
Is it always the last character you want to replace, or is there any other complexity involved, like only replace it duplicate, etc?

I find the regex and overkill way of doing this, you can easily do, an easy way to do will be inserting the number at second last index, and removing last character. 2 operations, yes, but simple and understandable.
Simos Sigma 21-Oct-18 5:03am    
Yes my friend, it's always the last letter(!!!) which I want to replace. Can I have an example of your solution please?

Based on your post, you can update the code to use the following example. The below example will replace the last character of a string with a corresponding alphabet number.

JavaScript
var s = "Filename AA";
s = s.replace(/\w([^\w]*)$/, "1");
console.log(s); //Filename A1

var s = "Filename A1";
s = s.replace(/\w([^\w]*)$/, "B");
console.log(s);  //Filename AB
 
Share this answer
 
Comments
Simos Sigma 21-Oct-18 5:11am    
Thank you for your answer my friend but with your example, all my filenames will change to 1 at it's last letter. I don't want this. Let's say I have this files...
Filename AA, Filename AB, Filename AC etc. What I want to do is this... Filename A1, Filename A2, Filename A3!!!
I finally found the solution...
JavaScript
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; 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900