Browse the docs

replaceFirst

Replaces only the first match of a regular expression in the text.

Details #

replaceFirst(text, pattern, replacement) takes exactly three arguments and returns text.

It works like replace, but stops after the first match. pattern is a regular expression (ICU syntax, as used by macOS), case-sensitive. In replacement, $1, $2 and so on insert capture groups, and $0 the whole match. If nothing matches, the text comes back unchanged.

Parameters #

Takes 3 arguments. {{ replaceFirst(text, b, c) }}

Examples #

replaceFirst("a-b-c", "-", "_")
→ a_b-c
replaceFirst("Smith, Jane", "([A-Za-z]+), ([A-Za-z]+)", "$2 $1")
→ Jane Smith
replaceFirst("  indented", "^ +", "")
→ indented

Gotchas #

  • A pattern that isn't valid stops the step: '(' isn't a valid regular expression.

  • Inside quotes a backslash followed by a letter drops the backslash ("\d" becomes d) except \n, \t and \r. Write "\\d" for the pattern \d, or use [0-9].

  • Keyboard Maestro has no function by this name, so nothing imports to it.