Saturday, January 5, 2019

javascript - What's the meaning of /gi in a regex?




I see an line in my JavaScript code like this:



var regex = /[^\w\s]/gi;


What's the meaning of this /gi in the regex?



Other part I can understand as it accepts a group of word and spaces, but not /gi.



Answer



g modifier: global. All matches (don't return on first match)

i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])


In your case though i is immaterial as you dont capture [a-zA-Z].



For input like !@#$ if g modifier is not there regex will return first match !See here.




If g is there it will return the whole or whatever it can match.See here


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...