so this exercise is about removing white space at the beginning and at the end of a string, so basically, I needed to use the white space shorthand and the pattern matchers caret ^ and dollar sign $, so my solution was this /^\s+|\s+$/ but it was not enough until I checked for hints and found the solution like down below:
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;;
let result = hello.replace(wsRegex,'');
my question is why they had to use the global flag to get it done?
I think there is only one pattern to match at the beginning ‘we use the caret for that’ and one pattern to match at the end of the string ‘we use the dollar sign for that’, why the g flag in this case? the challenge
Sorry, Because in one string there is only one beginning and one end. I’m little confused.
I can look at the string and i see 2 or 3 spaces at the begining and they are all matched with a caret and a gready match which is the short hand for white space followed by a + to matched longest consecutive single white spaces. Do you mean the regex matches only one white space the size of one character and the g flag is to match all the spaces at the begining? Either that or all the spaces at the begining are matched at once with gready match and therefore there is only one match with a caret to get at the begining. Where are all the other matches at the begining to be taken by the g flag?
The or flag simply says “find an occurrence in the string of either this substring or that one”. The g says “find all occurrences of all matches.” So even this:
const oneRegex = /[a-z]an/;
const allRegex = /[a-z]an/g;
const str = 'A man with a banana and a plan can handle anything';
// Array ['man']
// Array ["man", "ban", "lan", "can", "han"]
So without the g flag, the str.match() returns the first occurrence of the regex: 'man'. With the g, we get all matches.
Here is a question that will definitely answer my mind, so back to our first example with whitespace at begining and end of string, if i say i want to match only white space at the begining of the any string and then replace it … Should i use the g flag with my regex : /^\s+/g ? Or just just the regex /^\s+/ ?
This will really make things intuitive to me.
As that will match only the white space of a string, the global flag doesn’t mean much here. If we had this:
const myRegex = /^\s+/g;
that says “match any occurrence of spaces at the beginning of the string.” So where else in the string might you find “spaces at the beginning of the string?” In this case, the g flag doesn’t actually do a whole lot.
If you want to match all spaces, your regex would be this:
const myRegex = /\s+/g;
console.log(' The quick brown fox jumps over the
lazy dog '.match(myRegex) );
That last returns all ten whitespace groups. The first is a single space, the next two, then three… but the regex, because it’s g, finds all of them.
If we don’t want to find all the ones in the middle of the string, but just the ones at the ends, we can do that as you’ve seen:
const myRegex = /^\s+|\s+$/g;
that one says “find any whitespace characters not between words, but at the ends of the given string.”
Which come to my first understanding of it, the g flag is there in case we have whitespace in begining and whitespace at the end both accurring at same time, since the or operator is giving you one match either you will find it at the begining and at the end, so the g flag is here to get both matchs hence all match ( the definition of a g flag).
I feel what i said is wrong but i also feel i’m right.