Learn Form Validation by Building a Calorie Counter - Step 28

Strings have a .replace() method which allows you to replace characters in the string with another string. .replace takes two arguments. The first is the character sequence to replace – this can either be a string or a regex pattern. The second is the string to replace that sequence with. For example, this would replace all instances of l with 1:

"hello".replace(/l/g, "1");

Use your regex to replace all instances of +, -, and a space in str with an empty string. Return this value.

Your code so far

Here’s my solution:
function cleanInputString(str) {
const regex = str.replace(/[±\s]/g, “”);


The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 28


  • Leave the regex variable untouched.
  • Add a new line that directly returns the .replace() method used on str
  • Remember that .replace() takes two arguments, “regex” stores what you want to replace with an empty string!

Ok, I tried this (also without the “Return” line and both were incorrect):
function cleanInputString(str) {
const regex = /[±\s]/g;
str.replace(regex, “”);
return cleanInputString(str);

1 Like

You added two lines, “directly returns” means in the same line. Just wrap the .replace() in a return statement.


Thank you, it worked!

1 Like

just simply return

function cleanInputString(str) {
const regex = /[±\s]/g;
return str.replace(regex, “”);