importance of /g in Javascript regex
Posted: Sun Aug 07, 2005 6:50 am
I was having a doubt with using global search in Javascript regex cos I am able to search a big string without using /g.
ex:
rules:
1. a normal sentence which should not hv leading or trailing spaces
2. alphabets are only allowed
3. there can be space btw words
4. there should be an alphabet after a '\s'
var str = "this is a string";
var re =/^([a-z]+\s?[a-z]+)+$/i;
Can any of you help me understand the importance of /g with your own example?
ex:
rules:
1. a normal sentence which should not hv leading or trailing spaces
2. alphabets are only allowed
3. there can be space btw words
4. there should be an alphabet after a '\s'
var str = "this is a string";
var re =/^([a-z]+\s?[a-z]+)+$/i;
Can any of you help me understand the importance of /g with your own example?