Learn localStorage by Building a Todo App - Step 67

Hi :slight_smile:

Tell us what’s happening:

Even with the code I used in the Palindrome Checker to remove special characters, I still have these errors ‘removeSpecialChars should remove signle quotes / double quotes / underscores’.

I don’t understand what’s happen. When i log the value returned by my function with the parameter : ’ " Hi!_There’’ " the function returns “HiThere”.

My function has the behavior waited so I don’t see what I need to change to pass these tests.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

const removeSpecialChars = (string)=>{
  return string.replace("[^A-Za-z0-9]/g","")
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Challenge Information:

Learn localStorage by Building a Todo App - Step 67

Hi there!

Your regex syntax is not valide. Example:

\string\gi

You have quote marks around your regex pattern and also missing an opening forward slash. In character class you need to add a space symbol

Thanks Hasan, your answer was very helpful :slight_smile:

I tried again and again to find the solution and finaly I just made a regex which find single quote, double quote and underscore but i think this solution is less elegant than the solution which use this regex :

/[^a-zA-Z0-9]/g 

I tried to remove double quotes and add slashes as you mentioned so i obtained this code, but once again, the test didn’t pass :

return string.replace(/[^a-zA-Z0-9]/g,"")

I loged the result of this call :

removeSpecialChars("_"Hi! There'_");

And obtained this result : HiThere

The waited behavior is respected, so I still don’t understand why this solution is not accepeted ?

Because You haven’t added the white space character within the character class.

Ok I understand, so why this solution works ? As you see in my regex i don’t search for spaces, there is no \s metacharacter, but it works.

Sorry if I’m too insistant but I need to understand, I don’t see any logic in this test :crazy_face:

return string.replace(/[/'/'/"/"_]/g,"")

Joe

The code

return string.replace(/[/'/'/"/"_]/g,"")

is working because the regular expression includes characters like ', ", /, and _. This regex targets only these specific characters and removes each occurrence in the string.

The reason spaces are also removed because the curriculum challenge test are specific about the challenge instructions.

It’s still a bit confusing for me but thank you to have taking time to explain me why this test doesn’t pass with my first solution :slight_smile:
Joe

1 Like

the regex /[^a-zA-Z0-9]/g removes all non-alphanumeric characters, which could include spaces or other symbols that the challenge test might require to keep.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.