Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Regex Replace on SaveNew to Visual Studio Code? Get it now.
Regex Replace on Save

Regex Replace on Save

CryptoCooLby

|
993 installs
| (0) | Free
Automatically run RegExp replace text before saving a file.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Regex Replace on Save

Automatically run RegExp replace text before saving a file.

The motivation for this extension was to create auto formatting rules for Angular HTML templates in my projects. It could be used for many other purposes. PRs are welcome.

Options

  • File Extension to apply replace rules to
  • Find rule (regExp)
  • Replace rule

Example Configuration (settings.json):

  {
    ...

    "regexReplaceOnSave.ruleSets": {
    "html": {
      "Angular template interpolation padding start": {
        "find": "{{([^\\s])(.*)}}",
        "replace": "{{ $1$2}}"
      },
      "Angular template interpolation padding end": {
        "find": "{{(.*)([^\\s])}}",
        "replace": "{{$1$2 }}"
      },
      "Angular template pipe padding start": {
        "find": "([^\\s|^\\|])\\|",
        "replace": "$1 |"
      },
      "Angular template pipe padding end": {
        "find": "\\|([^\\s|^\\|])",
        "replace": "| $1"
      }
    }
  }

Under the hood

This function is called on each file with a matching file extension right before save.

  ...

  const newText = rules.reduce((text, rule) => {
    return text.replace(new RegExp(rule.find, 'g'), rule.replace);
  }, document.getText());
  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft