Need help with regex
I am working on a script to apply a hypertext marker to text with a specific character tag (Framemaker 19). The script will find the text with the tag, read the text, and apply the marker. The problem is that it applies the marker to any text with this character tag and I need it to only apply to certain text (F1, T2, etc.). This is why I'm trying to use regex, but it's not working. My script and notes are below. I'm been staring at this way too long. Can anyone see what I'm doing wrong?
//This first part finds text that has the character tag "Link." (I know it needs to be a function, but I'm having trouble with that, too, so I'll deal with it later.)
var doc = app.ActiveDoc;
var charFmt = doc.GetNamedCharFmt("Link");
var findParams = AllocatePropVals(2);
var fmtName = "Link";
var pgf = doc.FirstPgfInDoc;
findParams[0].propIdent.num = Constants.FS_FindCharTag;
findParams[0].propVal.valType = Constants.FT_String;
findParams[0].propVal.sval = fmtName;
findParams[1].propIdent.num = Constants.FS_FindWrap;
findParams[1].propVal.valType = Constants.FT_Integer;
findParams[1].propVal.ival = false;
var textRange = new TextRange();
textRange.beg.obj = textRange.end.obj = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
textRange.beg.offset = textRange.end.offset = 0;
doc.TextSelection = textRange;
textRange = doc.Find(textRange.beg, findParams);
// This calls a function to read the text
var num = getText(pgf, doc);
alert (num); // This works. It is getting and reading the text
// This is where I'm trying to use regex -- to match the variable "num"
var regex = /(?:Figure|Table) ((AF|AT|[FT])(\d+))/g; // Match figure or table number in text. Would there be a way to add this to the findParams section instead?
if (regex.test (num) === true) { //This is what doesn't work for me
createMarker (num);
}
// If I just call the function "createMarker (num)" without using the regex, it will apply the correct marker, but to whatever text is tagged "Link." So if the word "few" is tagged "Link", it will have the marker "gotolink few," which is not what we need.
function getText (pgf, doc) { //This works, reads the text
var text = "", textItems, i;
if (textRange.constructor.name !== "TextRange") { // Get a list of the strings in the text object or text range.
textItems = textRange.GetText(Constants.FTI_String);
} else {
textItems = doc.GetTextForRange(textRange, Constants.FTI_String);
}
// Concatenate the strings.
for (i = 0; i < textItems.len; i += 1) {
text += (textItems.sdata);
}
return text;
}
function createMarker (text) { // This works, creates the marker
var marker = doc.NewAnchoredObject(Constants.FO_Marker, (textRange.beg, textRange.end));
var markerType = doc.GetNamedObject(Constants.FO_MarkerType, "Hypertext");
marker.MarkerTypeId = markerType;
marker.MarkerText = "gotolink " + text.toLowerCase ();
return 1;
}
//Once I can get the regex working, the next step will be to make it search for the next instance of "Link," and then work its way through the document.
Thank you.
