These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Passing regular expressions through functions

JNorris
Registered: Sep 7 2011
Posts: 31
Answered

Pretty much, I want to search the pdf for bookmark names. If the name is matched then the form will display that it is included or missing in the file.
Used bookmark search through tutorial here.
 
Thanks in advance for the help :)
 
var inc = "Included";
var mis = "Missing";
var note = this.getField("TextField1");
 
function searchBookmarks(bkm, nLevel, bkmName)
{
if ( bkm.name == bkmName ) return bkm;
if (bkm.children != null) {
for (var i = 0; i < bkm.children.length; i++)
{
var bkMark = searchBookmarks(
bkm.children[i], nLevel + 1, bkmName);
if ( bkMark != null ) break;
}
return bkMark;
}
return null;
}
 
var bkmName = /\Wnote\W/;
var bkm = searchBookmarks(this.bookmarkRoot, 0, bkmName );
 
if ( bkm != null ) {note.value=inc;}
else {
note.value=mis;
}
   
(bkmName = "note"; works but I want to use an expression to find variations because of how different users type.)

My Product Information:
Acrobat Pro 10.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
You can do that, but you'd have to change the comparison that is performed within the function. You'd probably want to use the "match" string method and see if it's not equal to null, which would indicate a match.

It's not clear to me whether the regular expression you have is sufficient for what you want to do though. If searching for "note", do you also want to accept "Note" and "NOTE"? Would anything else be accepted?
JNorris
Registered: Sep 7 2011
Posts: 31
Don't know if it's just my version, but when users hit Enter after bookmarking a page, it creates a space so "note" is really "note ". Additionally, I would want to accept all cases and if there are any spaces/characters in front/behind the word.

If you could elaborate or provide a sample for your proposal, that would help a ton.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Just one more clarification since the regular expression depends on what you want to accept. When searching for "note" which, if any, of the following would match?

1. " Note " (more than one leading or trailing spaces)
2. "- note -"
3. "Note plus some other text"
4. "Some text plus note "
JNorris
Registered: Sep 7 2011
Posts: 31
All would be acceptable. "8989889note 89889" would be acceptable as well. By the way, thank you for the help. I really appreciate it.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
Try the following:

  1. var inc = "Included";
  2. var mis = "Missing";
  3. var note = this.getField("TextField1");
  4.  
  5. function searchBookmarks(bkm, nLevel, bkmName) {
  6.  
  7. // See if the current bookmark name matches the regular expression
  8. if (bkm.name.match(bkmName) != null) return bkm;
  9.  
  10. if (bkm.children != null) {
  11.  
  12. for (var i = 0; i < bkm.children.length; i++) {
  13.  
  14. var bkMark = searchBookmarks(bkm.children[i], nLevel + 1, bkmName);
  15.  
  16. if (bkMark != null) break;
  17. }
  18.  
  19. return bkMark;
  20. }
  21.  
  22. return null;
  23. }
  24.  
  25. // Define a regular expression
  26. var bkmName = /note/gi;
  27.  
  28. // Search the bookmarks names for a string matching the regular expression
  29. var bkm = searchBookmarks(this.bookmarkRoot, 0, bkmName);
  30.  
  31. if (bkm != null) {
  32. note.value = inc;
  33. } else {
  34. note.value = mis;
  35. }
If I understand what you want to allow, that regular expression should work. It won't if what you want to match is: any occurrence of the characters "note" (case insensitive) followed by at least one space, unless they are the last characters of the string
JNorris
Registered: Sep 7 2011
Posts: 31
Works like a charm. Pretty much I wanted to do was match any bookmark with the name "note" in it regardless or other characters surrounding it.

I learned a lot, George. Thanks. My errors were that I was not matching the strings and my expression wasn't appropriate.

Thanks again! :)