Count Phrase Occurrences in PDF Form Fields with Acrobat JavaScript
Question details
Count how many times a phrase entered in one PDF form field appears in article text stored in another field.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form contains an article text field, a search phrase field, and a field for displaying the count.
- Observed behavior
- The count field should display the number of occurrences of the entered phrase in the article field.
Open the PDF in Adobe Acrobat and confirm that the article text and search phrase are stored in separate form fields. Note their exact field names and create a third text field for the result if needed.
Add a JavaScript calculation to the PDF count field
Use a custom calculation to count literal, case-sensitive occurrences in the article form field.
This method searches text stored in a form field, not the visible text throughout the PDF. It counts non-overlapping matches and treats punctuation in the phrase as literal text.
In Acrobat, open the PDF and select Prepare a form from the tools or menu. Identify the article, phrase, and result text fields.
Open Properties for the result field, select the Calculate tab, choose Custom calculation script, and click Edit.
Paste the following script, replacing ArticleText and SearchPhrase with the exact names of your fields: var article = this.getField("ArticleText").valueAsString; var phrase = this.getField("SearchPhrase").valueAsString; if (phrase === "") { event.value = 0; } else { var escaped = phrase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); var matches = article.match(new RegExp(escaped, "g")); event.value = matches ? matches.length : 0; }
Confirm the script, close form editing, and save the PDF. Enter article text and a phrase in their fields, then move focus out of the edited field and check the result.

Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDF and Office files. Its format compatibility makes it easier to move existing documents into your workflow, though it does not replace the Acrobat-specific JavaScript calculation above.

Frequently Asked Questions
Does the script count matches in the whole PDF?
No. It searches only the text in the ArticleText form field. To search ordinary page text, you would first need to extract and assemble that text.
Is the phrase count case-sensitive?
Yes. The script uses the global regular-expression flag "g". Change it to "gi" in new RegExp(escaped, "g") to count matches without regard to case.
What happens if the search phrase contains punctuation?
The script escapes regular-expression special characters before searching, so characters such as periods, parentheses, and plus signs are matched literally.
Why does the result stay at zero when the phrase field is empty?
The calculation explicitly sets event.value to 0 for an empty phrase, avoiding meaningless empty-string matches.
What should I check if the count does not update?
Check that ArticleText and SearchPhrase exactly match the form field names, that the script is in the result field's Calculate tab, and that field calculations are enabled in Acrobat's form preferences.




