"Remove Whitespace from Start and End" Challenge, Alternate Solution

Challenge: Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.

Current Solution:

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

Proposed Alternate Solution:

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

The proposed alternate solution is not as elegant, but uses capture groups instead of the (x|x) method, which may be helpful to some. Is this a true alternate solution or a case of insufficient testing?

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