How to Combine String Replacement and Regular Expressions in Acrobat JavaScript
Question details
The user needs to manipulate text in a PDF form field using JavaScript to remove specific leading words, numbers, spaces, and punctuation before extracting the remaining characters.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Processing and formatting data extracted from PDF form fields to ensure clean text output.
- Observed behavior
- The user wants a clean text string stripped of unwanted prefixes, numbers, and whitespace to extract the correct remaining characters accurately.
Ensure your PDF form fields are properly named and that you have access to the JavaScript console or form properties to inject and test your scripts.
Use Chained Regular Expressions for String Replacement
Apply multiple .replace() functions sequentially to a form field's value to strip out unwanted characters.
By chaining replacement expressions in JavaScript, you can perform multiple text manipulations in a single line of code. This is particularly useful for sanitizing form data before processing it further.
Retrieve the text from your target field using: var OLDNAME = this.getField("OldbusinessName").valueAsString;
Add .replace(/^the/i, "") to ignore case and remove the word 'the' from the beginning of the string.
Append .replace(/\d+/g, "") to identify and remove any digits from the value.
Append .replace(/^\s+/, "") to clean up extra whitespace at the start of the final string.

Check Strings Conditionally Before Replacement
Use the .test() method if you only want to manipulate the string when it starts with specific words.
Easily Edit and Manage PDF Forms with WPS Office
While advanced JavaScript regex requires coding knowledge, WPS Office provides an intuitive PDF editor to easily manage form fields, edit text, and organize documents without complex scripting.
- 1. Open your PDF file: Launch WPS Office and open your PDF document containing the form fields.
- 2. Access the Form tab: Navigate to the Form tab on the top ribbon to view all interactive form controls.
- 3. Edit field properties: Right-click the target form field and select Properties to manage its formatting, default values, and actions.
- 4. Save the document: Click the Save icon or press Ctrl+S to preserve your form configurations securely.

Frequently Asked Questions
How do I open the JavaScript Console to test my regex?
You can typically access the JavaScript Console in Acrobat by pressing Ctrl+J (Windows) or Cmd+J (Mac) to test and debug your PDF scripts before finalizing them.
Does the order of chained replace methods matter?
Yes, the operations execute sequentially from left to right. Removing leading spaces before removing leading words might affect how your subsequent regular expressions match the remaining string.
Can I use JavaScript validation in WPS PDF?
WPS PDF supports standard interactive PDF forms. While advanced automated scripting may vary by software, standard PDF form fields created and validated with basic JS often function correctly across major PDF readers, including WPS Office.




