Regular Expressions, How do you remove white space with special characters?

Tell us what’s happening:
I tried \W but it didn’t work.
I don’t want to pass the test with Hello, World! I need to do the “appropriate string methods” (pass the test with special characters).

Your code so far


let hello = "   Hello, World!  ";
let wsRegex = /\b\w{5}\W+\s\D+\b/; // Change this line
let result = hello.match(wsRegex); // Change this line
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Remove Whitespace from Start and End

Link to the challenge:

If you don’t remember all the string methods that freeCodeCamp has introduced so far (or if you want to see what else is available) you can see the list and their descriptions here.

If you look at the page for .match(), you’ll see that it returns an array or null. So your result will not be a string.

Your regular expression is a bit peculiar. The task is the remove whitespace from the beginning and end of a string. There are regex symbols for whitespace and for “beginning of the string” and “end of the string”.

1 Like

How could /Hello, World!/ work but when doing it with special characters it doesn’t work?

you need to use the right special characters and the appropriate method together to have the desired effect

1 Like

Because your regular expression does not match “Hello World!”

See it in action here: https://regex101.com/r/EZdXoq/1

1 Like

Why doesn’t it work when I add \W+ in the end to match “!”

Please explain.

We would need to see your new code to help you. The tool I linked should help as well.

Ro reiterate though, the point is not to write something that matches “Hello, World!” You can write code that will trim the whitespace off of any arbitrary string.

1 Like

Thanks for showing me this tool, It’s very cool and useful.

I will try to figure that one out.

let hello = "   Hello, World!  ";
let wsRegex = /(?!=\s+)(?:\w+\D\s\w+\D)(?!=\s+)/; // Change this line
let result = hello; // Change this line

Why is this one not passing the test? On the tool it works.

Solution: Forgot to write down .match(wsRegex)

You will add this new solution to @camperbot at hint for this challenge? LINK

If you are interested in adding a new solution or hint you will need to create a new post in the #contributors section.

For contributions, please wrap your solution within :

[details]
```
code goes here...
```
[/details]

Also, provide all of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

You get money for sending in contributions?

No you don’t.

People volunteer to contribute to freecodecamp as a way to help the platform get better.

1 Like

I want to send in m contribution for free anyways because I’m to young to collect money anyways, I think 18 is when you can earn money.