Answer the question
In order to leave comments, you need to log in
How to replace a latin letter with a number in a string?
I have a string, at the end of which there can be any capital Latin letter from A to Z. Using a regular expression, I want to find if there is any letter at the end of the string, and if there is, replace it with a number (A for 1, B for 2 etc.).
let lastChar = str.match(/[AZ]/g); //returns a character
//look at what position this letter
is if (lastChar == str.length - 1) {
//if it is the last one, then replace it with the corresponding number
let symbolIndex = str.charCodeAt(-1) - 64;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question