E
E
Ekaterina07102021-08-10 13:24:05
JavaScript
Ekaterina0710, 2021-08-10 13:24:05

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

1 answer(s)
D
dodo512, 2021-08-10
@dodo512

.replace(/[A-Z]$/, m => m.charCodeAt() - 64)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question