Skip to content
Actions for Obsidian icon Home Actions for Obsidian 2024.1.3 › Actions

Actions for Obsidian › Actions › Search And Replace In A Note

Searches for a string or regex pattern in the specified note and replaces it.

This action will throw an error if the specified note doesn’t exist, so it’s best to use it after the “Check For Existence Of Note” action has determined that it does exist.

Parameters

Vault

The vault to work in.

Type: Vault reference

File Path

The path of the note, relative to the vault root.

Type: Text

Search term

Either a fixed string or a regular expression, depending on the setting “Search term is a regular expression”.

Regular expressions must be valid Javascript regex, see MDN or RegExr.com (unaffiliated) for info.

Type: Text

Optional Replacement

Type: Text

Search term is a regular expression

By default, the search term is searched for as-is. With this option enabled, the search term is treated as a regular expression instead, and must follow the format /search term/ (trailing flags (g, s, i, etc.) are allowed).

Type: Boolean (YES/NO)

Open or focus note

If enabled, after a successful operation, the note will be brought to the front in Obsidian.

Type: Boolean (YES/NO)

Examples

Suppose you have a note “Search and Replace demo.md” with the following content:

Today is a good day.

There’s a number of options — and lot of fun to be had! 😉

Searching for a string and replacing with another string

With the search term Today and the replacement Tomorrow, the result will be:

Tomorrow is a good day.
Screenshot of the action

Searching for a regular expression and replacing with a string

With a regex search term of /[oa]/ig (meaning: all “o” or “a” characters, case-insensitive) and the replacement string ui, the result is:

Tuiduiy is ui guiuid duiy.
Screenshot of the action

Searching for a regular expression and replacing with capture groups

Using regex together with capture groups is a powerful combination. For example, if a search term is /(my) (note)/, and the note in questions contains “my note”, then “my” and “note” will be available in the replacement as $1 and$2, respectively.

Here’s a concrete example: With a regex search term of /([oa])d([oa])/ig (meaning: an “o” or “a” character followed by a “d” followed by an “o” or “a” character, case-insensitive) and the replacement string $2d$1 (meaning: the second capture followed by a “d” followed by the first capture), the result is:

Tadoy is a good day.

Run it again:

Today is a good day.
Screenshot of the action