The regex looks for spaces at the start of the string or spaces at the end of the string. Without the g modifier, it will return only one match, but we want to trim both the spaces at the start and spaces at the end. (without g , it will trim only the start)
Thanks for explaining but I am still a little bit confuse. How does the replace method works? Does it mean that it checks the first conditions in the regex and replace the spaces in from of the string with “” then checks the second condition and does the same to the spaces at the back of the string? Or like does multiple condition means it checks all conditions at the same time?
How exactly it works would be written in a documentation.
But basically, it will go through the string and check if the conditions specified in the RegEx-string apply. Because it’s an “or” condition, it might check both on every character or be optimized to only check the second if the first returns false or identify that the first condition is for the start and the second for the end (funfact: you could switch those for the same result) - and hence ignore the first one once it’s beyond the “starting spaces”.
How exactly it works is written somewhere in the RegEx library, but the point of using it is to not needing to know this in detail