Remove Whitespace from Start and End
Problem Explanation
To solve this challenge you simply have to create a regex string that matches any spaces at the beginning or at the end of the string.
Hints
Hint 1
Think of how you can select substrings at the beginning or end of a string.
Hint 2
Think of how you can select whitespace
Solutions
Solution 1 (Click to Show/Hide) let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex…
/^\s+|\s+$/g
I am getting bit confused here, could someone explain why do I need to put the or in between ,shouldn’t the + look for multiple spaces and it is supposed to work without using |?
lasjorg
September 5, 2023, 7:19pm
2
Not if it is only the start and end spaces you have to match on.
' This has spaces '.match(/\s+/g)
// [' ', ' ', ' ', ' ']
' This has spaces '.match(/^\s+|\s+$/g)
// [' ', ' ']
Thank you so much. Much clearer now .
system
Closed
March 9, 2024, 7:11am
4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.