substitution rule type looks for specific patterns and suggests replacements. This is one of the most powerful rule types for enforcing terminology and style preferences.
How It Works
Thesubstitution rule searches for patterns defined in the swap map and suggests the corresponding replacement values. It automatically handles case preservation and can provide multiple replacement suggestions.
Parameters
A map where keys are patterns to match (can be regex) and values are suggested replacements. Values can use regex capture groups with
$1, $2, etc.Makes all matches case-insensitive when set to
true.Removes the default word boundaries (
\b) when set to true.An array of strings or patterns to be ignored.
If
true, Vale will use your vocabulary files as additional exceptions.If
true, Vale will capitalize the suggestion if the matched text is capitalized.Examples
Simple Replacement
Replace one term with another:Empty Replacement
Remove words entirely:Multiple Suggestions
Provide alternative replacements using the pipe operator:Regex with Capture Groups
Use capture groups to preserve parts of the match:Case-Insensitive with Capitalization
Match case-insensitively but preserve capitalization in suggestions:Use Cases
The
substitution rule is ideal for:- Enforcing brand name capitalization
- Replacing deprecated terms with preferred alternatives
- Standardizing technical terminology
- Suggesting more concise alternatives
- Enforcing inclusive language replacements
Technical Details
Internally, thesubstitution rule (internal/check/substitution.go:93-157):
- Sorts patterns by length (longest first) to avoid partial matches
- Converts capture groups to non-capturing groups automatically
- Compiles all patterns into a single alternation regex
- For each match, looks up the corresponding replacement
- Handles case preservation if
capitalizeis enabled - Splits pipe-separated replacements into multiple suggestions
FindAllStringSubmatchIndex to capture both the match and any subgroups, allowing for complex pattern-to-replacement mappings.
Message Formatting
When using theaction.name: replace with multiple suggestions (pipe-separated), Vale automatically:
- Converts the message format to avoid double-quoting
- Creates an array of replacement options
- Formats the message as “Use X, Y, or Z instead of ‘%s‘“
Related Rule Types
- existence: Use when you only want to flag terms without suggesting replacements
- consistency: Use when you want to enforce consistency between variant spellings
- capitalization: Use when you want to enforce specific case styles