Skip to content

Replace multiple strings in files

Actions
Find and replace multiple strings over matching files
v2.0.630
Latest
Star (2)

Tags

 (1)

Multiple string find/replace in files

build-test

This action searches matching files for strings and replaces any matches with replacement values, then saves the modified file(s).

Usage

steps:
      - uses: actions/checkout@v5
      - uses: flcdrg/replace-multiple-action@v2
        with:
          files: './*.md'
          find: '[{ "find": "Multiple", "replace": "Many" }]'

Example updating links in Markdown

Search for Markdown files in the current directory. In each matching file, search for Markdown links. eg. that start the line, have whitespace or a left bracket preceding them, and have an end of line, whitespace or right bracket immediately after them.

steps:
  - uses: flcdrg/replace-multiple-action@v2
    with:
        files: './*.md'
        find: '[{ "find": "http://localhost", "replace": "https://localhost"}, { "find": "http://davidgardiner.net.au", "replace": "https://david.gardiner.net.au" }]'
        prefix: '(^|\\s+|\\()'
        suffix: '($|\\s+|\\))'

Example with Wayback machine action

The replacements output from the flcdrg/wayback-machine-query-action action is the correct shape to be passed in to the find input for this action.

steps:
  - name: Wayback Machine Query
    uses: flcdrg/wayback-machine-query-action@v2
    id: wayback
    with:
        source-path: ./lychee/links.json
        timestamp-regex: '_posts\/(\d+)\/(?<year>\d+)-(?<month>\d+)-(?<day>\d+)-'
        
  - name: Replacements
    uses: flcdrg/replace-multiple-action@v2
    with:
        find: ${{ steps.wayback.outputs.replacements }}
        prefix: '(^|\\s+|\\()'
        suffix: '($|\\s+|\\))'

Inputs

Various inputs are defined in action.yml to let you configure this action:

Name Description Default
files A wildcard glob used to match files that should be searched **/*.md
find A JSON array of strings to find and values to replace. The JSON array should conform to this structure. { find: string; replace: string; }[]
prefix (optional) Part of a regular expression that is prepended to the search string to add additional context on when to match the string. The matched value of this regular expression group also prepended to the replacement value.
suffix (optional) Part of a regular expression that is appended to the search string to add additional context on when to match the string. The matched value of this regular expression group is also appended to the replacement value.

Replace multiple strings in files is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.

About

Find and replace multiple strings over matching files
v2.0.630
Latest

Tags

 (1)

Replace multiple strings in files is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.