-2

I have a string of digits and letters like this:

let cad = "123941A120"

I need to convert that with these substitutions: A = 10, B = 11, C = 12, …, Z = 35. For example, the string above would result in the following, with A replaced by 10: 12394110120.

Another example:

Input:  158A52C3
Output: 1581052123
Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
  • 1
    '123941A120'.replace('A', 10);` – Randy Casburn Feb 20 '21 at 03:02
  • 2
    Voting to reopen. I disagree that a question asking how to convert base-36 digits to a string of base-10 numbers is “answered” by a general regex reference. @Wiktor, I know that you usually vote to close questions asking “How to write this regex?” with this duplicate target (usually accompanied by a comment, explaining how one can learn to write a regex oneself). While this is usually relevant and applicable to most such questions, for this one, regex may not even be the appropriate tool, and the extra numeric conversion step is definitely not covered by the target. – Sebastian Simon Feb 23 '21 at 19:53
  • 1
    This is not a valid question to reopen. Questions that ask ["Give me a regex that does X"](https://meta.stackoverflow.com/q/285733) with no attempt are off topic on Stack Overflow. Also, see [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236) – Wiktor Stribiżew Feb 23 '21 at 19:54
  • I disagree with the general practice of redirecting beginner regex users to that list of links. It is almost useless to a beginner. If these style of questions are off topic we should redirect them directly to a known beginner-friendly tutorial. I know there are tutorials listed on that wall of links page but it's not quite the same... – xdhmoore Feb 23 '21 at 23:16
  • 1
    And to @SebastianSimons point, I agree that this question has some non-regex-related merit on its own for future searches, the most elegant solution not involving regex. – xdhmoore Feb 23 '21 at 23:20

4 Answers4

1

What you’re trying to do is to convert each digit to base 10. As each digit is from the range 0, 1, …, 8, 9, A, B, …, Y, Z, you’re dealing with a base-36 string. Therefore, parseInt can be used:

const convertBase36DigitsToBase10 = (input) => Array.from(input, (digit) => {
    const convertedDigit = parseInt(digit, 36);
    
    return (isNaN(convertedDigit)
      ? digit
      : String(convertedDigit));
  }).join("");

console.log(convertBase36DigitsToBase10("158A52C3")); // "1581052123"
console.log(convertBase36DigitsToBase10("Hello, world!")); // "1714212124, 3224272113!"

If you really want to stick to regex, the answer by xdhmoore is a good starting point.

Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
0

You can do:

const arr = [
  { A: 10 },
  { B: 11 },
  { C: 12 },
  // ...
]

const input = '158A52C3'
const output = arr.reduce((a, c) => {
  const [[k, v]] = Object.entries(c)
  return a.replace(new RegExp(k, 'g'), v)
}, input)

console.log(output)
Yosvel Quintero Arguelles
  • 15,149
  • 4
  • 33
  • 35
0

This will do it without having to map all the letter codes, with the assumption that adjacent letters have adjacent codes...

result = "158A52c3".replaceAll(/[A-Z]/ig, (c) => {
    offset = 10;
    return c.toUpperCase().charCodeAt(0) - "A".charCodeAt(0) + offset;
})
    
console.log(result);
xdhmoore
  • 6,443
  • 6
  • 37
  • 71
0

u can do the following:

const letters = {
'A':10,
'B':11,
'C':12
}
let cad = '123941A120'
for(let L in letters){
cad = can.replace(L,letters[L])
}
console.log(cad)