Change Javascript string characters for successors

Asked

Viewed 58 times

2

I have a string, for example, acbd and want to get another string bdce, formed by the successors of each character.

In my searches find a possible way that is to transform the string into an array and then iterate it to get a new string.

var r = "acbd".split("").map(function(a) { 
  return String.fromCharCode(a.charCodeAt(0) + 1)
}).reduce(function(p, c) {
  return p + c
});

console.log(r);

However, I’m trying to figure out a possible algorithm to switch characters to their successors, without turning the string into an array using the method replace with a RegExp. Could someone help me?

1 answer

2


Here is the code:

var r = "abcd".replace(/[a-zA-Z]/g, function myFunction(x){
  return String.fromCharCode(x.charCodeAt(0)+1);
});
console.log(r);

  • Blz that’s right, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.