InDesign脚本求助:将e.g.格式统一替换为带细空格的规范写法
I’ve got you covered! Below are two reliable methods to replace both space-less (e.g.) and regular-space (e. g.) versions of "e.g." with the standardized e. g. (using a hair space, U+200A) in your InDesign document—one using built-in GREP find/change, and another using a standalone JavaScript script that avoids relying on saved queries.
Method 1: Manual GREP Find/Change (No Script Needed)
If you prefer a quick manual approach without scripting:
- Open the Find/Change panel (
Ctrl+ForCmd+F) and switch to the GREP tab. - In the Find what field, enter:
e\. ?g\.- Breakdown:
e\.matches the literal "e." (the backslash escapes the dot, which otherwise matches any character)?matches 0 or 1 of the preceding character (so it catches both no space and one regular space)g\.matches the literal "g."
- Breakdown:
- In the Change to field, enter:
e.\x{200A}g.\x{200A}is InDesign’s GREP syntax for the hair space Unicode character (U+200A)
- Click Change All to apply the fix across your document.
Method 2: JavaScript Script (Automated Workflow)
If you need to automate this (or integrate it into a larger script), here’s a complete, self-contained script that doesn’t require pre-saved find/change queries:
// Check if there's an active document if (app.documents.length > 0) { var doc = app.activeDocument; // Reset find/change preferences to avoid leftover settings app.findGrepPreferences = NothingEnum.NOTHING; app.changeGrepPreferences = NothingEnum.NOTHING; // Set up GREP find parameters app.findGrepPreferences.findWhat = "e\\. ?g\\."; // Double backslashes for JS string escaping app.findGrepPreferences.searchMode = SearchModes.grepSearch; // Set up GREP change parameters (hair space via Unicode escape) app.changeGrepPreferences.changeTo = "e.\\x{200A}g."; // Execute the change and get count of replacements var changesMade = doc.changeGrep(); alert(`Replaced ${changesMade} instances of e.g./e. g. with the standardized format!`); } else { alert("Please open an InDesign document first!"); }
Quick Notes on the Script:
- Double backslashes (
\\) are required in JavaScript to escape the dot for the GREP pattern (since JS uses backslashes as escape characters). \\x{200A}correctly references the hair space character in InDesign’s change field.- The script includes basic error handling to avoid crashes if no document is open, and it shows a count of changes made for verification.
Either method will ensure all instances of "e.g." (with or without a regular space) are replaced with your company’s standardized format using a hair space.
内容的提问来源于stack exchange,提问作者Bettie Booze

