Regular Expressions - Remove Whitespace from Start and End

I’m not sure why my solution doesn’t work. In my mind wsRegex begins at the beginning of the string, matches with anything that is not a whitespace, then it matches with anything that is a whitespace, then it matches with anything that is not whitespace and finally returns everything it matched with to result. I know the solution posted finds the whitespaces and replaces them with nothing. My idea for the solution was find everything that is not a whitespace and return that (except for whitespaces in the middle of the word).

Your code so far

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

console.log(result);

Your browser information:

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

Challenge: Regular Expressions - Remove Whitespace from Start and End

Link to the challenge:

let hello = " Hello, World! ";
let wsRegex = /^\S*\s*\S*$/g;

Your regular expression is saying:

  • At the beginning of the string match a non-whitespace character zero or more times
  • Then, match a whitespace character zero or more times
  • And at the end of the string match a non-whitespace character zero or more times
  • global flag activated (return all possible matches)

Your hello string begins with a whitespace character, that’s why your regex can’t find a match from the very beginning.

Ah I see, but why won’t it keep searching until it finds a non-whitespace character at the beginning? Is the string not evaluated in a left-right fashion?

Your regex is trying to exactly match as I described to you. If it fails to match even for the first type of character, it will totally fail.

Ah I see, thank you for clearing that up! Also is there a way to evaluate the whitespaces but not include them in the match? Like could I use a positive lookahead to evaluate white spaces at the beginning? That way my function won’t be stopped.

Yes, there is. But, if you are really at the beginning of regular expressions, I would recommend you to learn the basics. FreeCodeCamp is really a good platform to start fresh in coding.

In this case, what you want is to match a first non-whitespace character one or more times, match a whitespace character, and finally match a non-whitespace character one or more times.

Just try without the caret (^) and the dollar sign ($). Those are restricting you.

I tried let wsRegex = /(?=\s*)\S*\s\S*/g; and result = [ ’ ‘, ’ ‘, ’ Hello,’, ’ World!’, ’ ', ’ ’ ]. Why are there spaces at the beginning and the end if I’m not matching those?

Try the website regexr.com . There you can simulate more easily your regular expressions, and see each change immediately.

Will do, thanks! I figured out why it wasn’t working. The positive lookahead only works after you have something that matched. At least thats what I found.

1 Like
/(?=\s*)\S*\s\S*/g

You wrote a positive lookahead before the expression you want to find. In that position, you would have to write a positive lookbehind:

/(?<=\s*)\S*\s\S*/g

There are always several ways to achieve the same coding goal.

P.S.: I do not know if I confused you, but:
* matches a preceding character zero or more times
+ matches a preceding character one or more times

No confusion! Thanks for helping me! I didn’t know there was a lookbehind. That should do the trick :grin:. Talking through this with you has really helped me understand it more! Thx!

1 Like

No problem :slight_smile: . If you passed the challenge and don’t have more questions on this subject, please select this topic question as solved. Thank you