I have created a Caesar Cipher code below but I want the returned string to include spaces and other characters. I've tried regex but this does not seem to solve the problem or maybe i'm not using it right, I'm not too sure.
Any help appreciated. Thanks!
function caesarCipher(str, n) {
let newStr = '';
let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
let regex = /[a-z]/
for (let i = 0; i < str.length; i++) {
if (str[i].match(regex) === false) {
newStr += str[i]
continue;
}
let currentIndex = alphabet.indexOf(str[i]);
let newIndex = currentIndex + n;
newStr += alphabet[newIndex];
}
return newStr
}
console.log(caesarCipher('ebiil tloia!', 3)) //should return hello world! but returns hellocworldc