Hi,
I'm having trouble with the RegExp object's exec() method, which according to the docs shoudl return an array of all matches. For me, it only returns the first match.
I am trying to use it to identify the cameras in a .duf file. There are two instances of the string "camera" in the file - one as such, and one as "current_camera", but exec() only returns the first when using the pattern "camera".
var filedir = new DzDir("C:/Users/blah/Documents/DAZ 3D/Studio/My Library/Scenes");
var filename = new DzGZFile(filedir.path() + "/TestScene01.duf");
if ( ! filename.exists() ) {
print("File not found");
}
if ( filename.open( DzFile.ReadOnly ) ) {
print(String("File opened, size %1").arg(filename.size()));
var lines = filename.read();
print("Length " + lines.length);
var obj = JSON.parse(lines);
var dufstr = JSON.stringify(obj);
for (var ok = 0; ok < Object.keys(obj).length; ok++ ) {
print(Object.keys(obj)[ok]);
}
//var rg = new RegExp("camera","gm");
//var rg = /camera/gm;
var rg = new RegExp( /camera/gm );
var aCams = rg.exec(dufstr);
print("aCams length = " + aCams.length);
print("idx = " + rg.search(dufstr));
for (var x = 0; x < aCams.length; x++) {
print("x: " + x + " - " + aCams[x]);
}
}
No matter how I define the regex, and with or without the g and m pattern flags, the results I get are these:
File opened, size 17297
Length 195878
file_version
asset_info
geometry_library
node_library
uv_set_library
image_library
material_library
scene
Dufstuff length = undefined
aCams length = 1
idx = 11858
x: 0 - camera
Result:
Script executed in 0 secs 22 msecs.
As can be seen, only the first occurence of "camera" is returned, which seems to be incorrect.
Any help/suggestions/corrections appreciated.