I worked on the below challenge for about 3 hours and none of my code was working. Decided to look at the solution to understand why I was not working. When I looked at the solution I was confused because I thought that \s to identify white spaces not to remove them... can someone give me hand and explain why the usage of \s instead of \S and why using the empty string ("") to get rid of the white spaces on both ends.
CHALLENGE
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
//SOLUTION
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
I worked on the below challenge for about 3 hours and none of my code was working. Decided to look at the solution to understand why I was not working. When I looked at the solution I was confused because I thought that \s to identify white spaces not to remove them... can someone give me hand and explain why the usage of \s instead of \S and why using the empty string ("") to get rid of the white spaces on both ends.
CHALLENGE
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
//SOLUTION
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
Share
Improve this question
edited Jun 16, 2021 at 23:10
PM 77-1
13.3k21 gold badges71 silver badges115 bronze badges
asked Jun 16, 2021 at 23:07
Cristian M.Cristian M.
771 gold badge2 silver badges7 bronze badges
3
- 1 The code identifies white space characters and then replaces them with empty string. By replacing something with nothing we get the same result as deleting something. – PM 77-1 Commented Jun 16, 2021 at 23:11
- It's finding whitespace and replacing what it finds with an empty string, aka removing it – Andy Ray Commented Jun 16, 2021 at 23:12
- 1 .... you decided to implement .trim() function – Amir Azizkhani Commented Feb 14, 2023 at 4:43
3 Answers
Reset to default 12\s
means whitespace characters in regex, like space, tab, etc.^
means the beginning of the string$
means the end of the string|
means OR (match the left side or the right side)+
means 1 or more (based off of the rule on the left)/a regex/g
theg
means "global", aka "match multiple times" since you could need to match at the beginning AND end
So the regex means:
/^\s+|\s+$/g
/ / Wrap the regex (how you do it in JS)
^\s+ Try to match at the beginning one or more whitespace chars
| Or...
\s+$ Try to match whitespace chars at the end
g Match as many times as you can
String.prototype.replace replaces the match(es) found in the regex with the string provided as the 2nd argument, in this case an empty string.
So the process internally is:
- Look for all sections that match the regex (which will be the whitespace at the beginning and the whitespace at the end
- Replace each match with
""
, removing those matches entirely
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
console.log('"' + result + '"');
Most people use String.prototype.replaceAll instead of .replace
when they use the global flag
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replaceAll(wsRegex, "");
console.log('"' + result + '"');
The second argument of replace
is for what you will replace from the match(es) of the first argument.
The regex will match/select the spaces on the beginning (^) and on the end ($) of the string, and then will be replaced by "".
When you use the regex /(\S)/g
you're matching everything but spaces, in this case you will use something like hello.replace(/(\S)/g, '$1')
;
$1
means the first group of your regex.
let str = "__ @!Hello World___-!! "
console.log(str); //"__ @!Hello World___-!! "
console.log(str.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g, '')); //"Hello World"