views:

121

answers:

3

Here is the case: I want to find the elements which match the regex...

targetText = "SomeT1extSomeT2extSomeT3extSomeT4extSomeT5extSomeT6ext"

and I use the regex in javascript like this

reg = new RegExp(/e(.*?)e/g);   
var result = reg.exec(targetText);

and I only get the first one, but not the follow.... I can get the T1 only, but not T2, T3 ... ...

+1  A: 
targetText = "SomeT1extSomeT2extSomeT3extSomeT4extSomeT5extSomeT6ext"    
reg = new RegExp(/e(.*?)e/g);   
var result;
while (result = reg.exec(targetText))
{
    ...
}
port-zero
+1  A: 
reg = new RegExp(/e(.*?)e/g);   
var result;
while((result = reg.exec(targetText)) !== null) {
    doSomethingWith(result);
}
chaos
+3  A: 

Try using match() on the string instead of exec(), though you could loop with exec as well. Match should give you the all the matches at one go. I think you can omit the global specifier as well.

reg = new RegExp(/e(.*?)e/);   
var matches = targetText.match(reg);
tvanfosson