JavaScript:
.replace(/_/g," ");
I have it in my code but can't remember why or what it does! Can one of you regular expression gurus help?
I know this may seem basic, but regular expressions are not my cup of tea and googling for /g
didn't help much.
The " g " flag indicates that the regular expression should be tested against all possible matches in a string. A regular expression defined as both global (" g ") and sticky (" y ") will ignore the global flag and perform sticky matches. You cannot change this property directly.
\s means "one space", and \s+ means "one or more spaces". But, because you're using the /g flag (replace all occurrences) and replacing with the empty string, your two expressions have the same effect. Follow this answer to receive notifications.
The "g" modifier specifies a global match. A global match finds all matches (compared to only the first).
replace in JavaScript will only replace the first matching value it finds. Adding the /g will mean that all of the matching values are replaced. Show activity on this post. The "g" that you are talking about at the end of your regular expression is called a "modifier".
The regex matches the _
character.
The g
means Global, and causes the replace
call to replace all matches, not just the first one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With