It looks like this method can by simplified to just a few calls to .replace
:
function LetterChanges(str) {
return str
.replace(/[a-y]|(z)/gi, function(c, z) { return z ? 'a' : String.fromCharCode(c.charCodeAt(0)+1); })
.replace(/[aeiou]/g, function(c) { return x.toUpperCase(); });
}
LetterChanges("abcdefgxyz");
// "bcdEfghyzA"
Or alternatively, a single call to .replace
, like this:
function LetterChanges(str) {
return str.replace(/(z)|([dhnt])|[a-y]/gi, function(c, z, v) {
c = z ? 'A' : String.fromCharCode(c.charCodeAt(0)+1);
return v ? c.toUpperCase() : c;
})
}
LetterChanges("abcdefgxyz");
// "bcdEfghyzA"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…