let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex,""); // Change this line
Explanation :(/^\s+|\s+$/g;)
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex,""); // Change this line
Explanation :(/^\s+|\s+$/g;)
- caret symbol ^ looks at the start of string
- dollar symbol $ looks at the end of string
- The | is an OR operator
- \s+ looks for one or more white spaces
- The g flag is needed to match all the spaces that are at the beginning OR at the end. Otherwise it would match only the first part of the regex, and the second part only if the first part doesn’t match anything