Removing whitespaces

Tell us what’s happening:
Describe your issue in detail here.
I tried to solve this one with the .match property unlike the solution and my code seems correct to me but apparently I am missing something since it gives an error. I would appreciate any help.

Your code so far

let hello = "   Hello, World!  ";
let wsRegex = /[^[(^\s)+(\s+$)]]/; // Change this line
let result = hello.match(wsRegex); // Change this line

Your browser information:

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

Challenge: Remove Whitespace from Start and End

Link to the challenge:

Have you looked at what result variable contain at the end? You may add console.log(result) to see it.

I’d also suggest using a tool like regex101 when constructing your regex.

1 Like

it returns null when I do that.

match returns null if there is no match.

Yes but I don’t understand why there is no match.

Use the tool I linked to. Add your regex and read the information on the right side. Expand the explanations (twirl open the >). It explains what you are matching against.

1 Like

so what I am trying to do is ignoring the whitespaces at the beginning and the end and then match it with the original string. I used the tool you recommended and it tells me to escape a delimiter but I don’t get why I would try to escape a character at this point since it is used for searching for special characters (at least that is what I understood from the link you attached. Here is what I have written so far:

let hello = "   Hello, World!  ";
let wsRegex = /[^(^\s+\s+$)]/g; // Change this line
let result = hello.match(wsRegex); // Change this line

I think you need to see again what the square brackets do
What do you think they to do?

doesn’t this [^ mean “don’t match the rest” ? That’s what it seemed to me in the " Match Single Characters Not Specified" challenge.

I figured out my mistake by checking with console.log but when I use console.log(result) for this code it returns the desired string but does not pass the test, why?

let wsRegex = /^\s+\s+$/g; // Change this line
let result = hello.replace(wsRegex,""); // Change this line

Porque no solo usas el metodo propio de las cadenas “String”, el metodo “trim”

this means one or more spaces at the beginning followed by one or more spaces at the end. Or a string made only of spaces.
The string doesn’t change at all, result and hello are still equal

1 Like

Hay que fijarse tambien en el tamaño o la longuitud de la cadena, la respuesta correcta no debe tocar el espacio entre las palabras, solo los extremos. Bueno en ese error estaba caendo, no lo habia tomado en cuenta.

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