Skip to content

Feature/2580 support regex for templating - #2668

Merged
JoernBerkefeld merged 3 commits into
developfrom
feature/2580-support-regex-for-templating
Oct 4, 2026
Merged

JoernBerkefeld merged 3 commits into
developfrom
feature/2580-support-regex-for-templating

Conversation

@JoernBerkefeld

Copy link
Copy Markdown
Contributor

PR details

What changes did you make? (Give an overview)

Further details (optional)

Regex market variables

Existing scalar market variables remain compatible: strings, numbers, and booleans use the existing literal replacement behavior. You can mix scalar variables and regex objects in the same market. The special handling of description and the existing fallback behavior remain unchanged.

For more selective matches, configure a variable as an object:

  • search: a JavaScript regular expression source, without / delimiters or flags. It is applied globally when building a template (buildTemplate / bt), replacing every match.
  • replace: a literal string, number, or boolean used when building a definition (buildDefinition / bd). It is not a JavaScript capture-group replacement expression.
  • template (optional): a native JavaScript replacement string used when building the template. Capture references such as $1 and $2 preserve parts of the match. If omitted, it defaults to {{{key}}}, where key is the market variable's name.

Match an exact quoted value

The following .mcdevrc.json example matches the complete quoted value "Dynamic" rather than every occurrence of Dynamic:

{
    "markets": {
        "source": {
            "senderProfile": {
                "search": "(\")Dynamic(\")",
                "replace": "Dynamic",
                "template": "$1{{{senderProfile}}}$2"
            }
        },
        "target": {
            "senderProfile": {
                "search": "(\")Transactional(\")",
                "replace": "Transactional",
                "template": "$1{{{senderProfile}}}$2"
            }
        }
    }
}

The first and second capture groups contain the surrounding quotes. The replacement $1{{{senderProfile}}}$2 therefore produces "{{{senderProfile}}}", keeping the JSON template valid. Values such as "Dynamic Sender Profile" and "OrderConfirmationDynamic_DEV" remain unchanged. Building with the source market restores "Dynamic"; building with the target market produces "Transactional". The target's search lets that value be templated again using the same variable.

Match a sender profile field prefix

To replace only the local-part prefix in FromAddress, while leaving occurrences in other fields alone, use this .mcdevrc.json example:

{
    "markets": {
        "source": {
            "regex": {
                "search": "(\"FromAddress\":\\s*\")joern",
                "replace": "joern",
                "template": "$1{{{regex}}}"
            }
        },
        "target": {
            "regex": {
                "search": "(\"FromAddress\":\\s*\")thuy",
                "replace": "thuy",
                "template": "$1{{{regex}}}"
            }
        }
    }
}

For example, "FromAddress":"[email protected]" becomes "FromAddress":"{{{regex}}}@example.com". The capture group preserves the field name, colon, optional whitespace, and opening quote; the domain and closing quote are outside the match and stay intact. Building for the source restores [email protected]; building for the target produces [email protected].

Important: match the text mcdev actually searches. JSON metadata is searched as compact JSON.stringify output, not the pretty-printed file on disk. There is no space after a colon in that serialized output, so a pattern containing the literal space in "FromAddress": "joern will not match it. The JSON regex source "(\"FromAddress\":\\s*\")joern" allows zero whitespace as well as whitespace when present. Write \\s* in JSON so the parsed regular expression receives \s*.

Embedded newlines in JSON string values are escaped in the serialized metadata; they are not actual line breaks for a whitespace regex to match. Extracted code files, on the other hand, retain actual whitespace and newlines, so \s* can match those characters there.

These are text-only replacements. A numeric or boolean replace value renders as text; native JSON type-preserving templating of number or boolean fields is out of scope. Keep the surrounding syntax valid, including quotes and any required escaping in JSON strings.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • test scripts updated
  • Wiki updated (if applicable)

@JoernBerkefeld JoernBerkefeld self-assigned this Oct 4, 2026
@github-actions github-actions Bot added the enhancement New feature or request; requires increasing the minor version of mcdev. Jira issue-type "Story" label Oct 4, 2026
@JoernBerkefeld JoernBerkefeld linked an issue Oct 4, 2026 that may be closed by this pull request
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

Coverage Report

Commit:721e988
Base: develop@5d878a7

Type Base This PR
Total Statements Coverage  76.23%  76.24%  (+0.01%)
Total Branches Coverage  73.54%  73.63%  (+0.09%)
Total Functions Coverage  86.73%  86.73%  (+0%)
Total Lines Coverage  76.23%  76.24%  (+0.01%)
Details (changed files):
File Statements Branches Functions Lines
lib/metadataTypes/MetadataType.js  85.68%  83.18%  77.77%  85.68%
lib/util/util.js  87.5%  78.29%  93.22%  87.5%

@JoernBerkefeld JoernBerkefeld added this to the 10.0.0 milestone Oct 4, 2026
@JoernBerkefeld
JoernBerkefeld merged commit 6a92ec3 into develop Oct 4, 2026
9 checks passed
@JoernBerkefeld
JoernBerkefeld deleted the feature/2580-support-regex-for-templating branch October 4, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request; requires increasing the minor version of mcdev. Jira issue-type "Story"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] support regex for templating

1 participant