Tell us what’s happening:
I’m trying to find out why my special character regex isn’t working.
I’ve tried a lot of different regex patterns here but none seem to work on the whitespaces and special characters.
Is there something that blocks me from using regex here?
I can get code working by hard coding all the special characters in tests but that surely is not what is sought after.
Your code so far
function rot13(str) {
//create variables for alphabet and empty string
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let deciphered = "";
//added regex variable for readability
let specialsRegex = /[^A-Z]/g
//loop through the string and find letter location on alphabet
for (let i = 0; i < str.length; i++) {
//add spaces or other special characters (figured out I should be able to use regex here)
if (str[i] == specialsRegex) {
deciphered += str[i];
}
for (let j = 0; j < alphabet.length; j++) {
if (str[i] === alphabet[j]) {
if (j + 13 < alphabet.length) {
deciphered += alphabet[j + 13];
} else {
deciphered += alphabet[j - 13];
}
}
}
}
console.log(deciphered)
return deciphered;
}
rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");
rot13("SERR YBIR?");
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher
The line above is checking where a character in the string is equal to a regular expression. These will NEVER be equal. What did you think this was checking or hoping to check?
It is great that you solved the challenge, but instead of just giving a solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
I’ve solved the challenge but I kind of had to resort in hard coding some regex cases.
I was not able to make a single regex that would cover:
1 555-555-5555 = true
1 (555) 555-5555 = true
1(555)555-5555 = true
1 555 555 5555 = true
1 555)555-5555 = false
It makes me feel like I took a shortcut there and I’m really interested in getting some advice what should I do to match more cases.
I’m sure there is a way to cover all the example cases I mentioned with a single regex because at one point I only had one test case left to tackle but the regex got so complicated I totally lost track what happens when and where.
this in itself is a reason not to bother with “one regex to rule them all”! (I hope you are a Lord of the Rings fan…)
You can use the or character | to separate different groups or you can use lookaheads but in the end, if your regex is unreadable to you, then that is not useful.
@antisankari Please do not ask a question that is for another challenge on the same thread. Please start another topic for your other challenge question.