Hi Klaus,
Thanks for posting here. With JavaScript regular expressions, you have to use the "g" flag to get it to find more than the first occurrence (g = global).
#target framemaker
var pgfText = "Hello fans [[Dante, #712]] and another one [[DuçanÌsídõrâ, #312]].";
var citations = getCitations (pgfText);
alert(citations);
function getCitations (pgfText) {
// Regular expression to isolate the citations. var regex = /(\[\[[^\]]+\]\])/g; // Array to store the citations. var citations = [], result; // Execute the regular expression. while (result = regex.exec (pgfText)) { // Push the result onto the array. citations.push (result[1]); } // Return the array return citations;
}
You can see where I have added the g flag at the end of the regular expression. Then I execute the regular expression in a loop; the loop will continue as long as there are matches in the string. For each match, I push the string into the array. When all strings are found, I return the array from the function.
A couple of notes: I like to use the #target framemaker instruction at the top of my scripts to ensure that they run with the FrameMaker object model instead of the default ExtendScript Toolkit. (In this case, it doesn't matter, because this is all native JavaScript code and not dependent on FrameMaker.) Also, you should always declare your variables with the var keyword.
Please let me know if there are any questions or comments.
-- Rick