replaceAll
Learn how to use the replaceAll function within a Notion formula.
The
replaceAll()
function searches a string for a pattern (which can be a regular expression), and replaces ALL matches it finds with another string.1
replaceAll(string, string [regex supported], string [regex supported])
Since they can search for pattern matches using regular expressions,
replaceAll()
and its counterpart, replace, are two of the most versatile and powerful functions you can use in your Notion formulas.1
replaceAll("Dogs Dogs Dogs","Dogs","Cats") // Output: Cats Cats Cats
2
3
// Matches are case-sensitive
4
replaceAll("Dogs dogs Dogs","Dogs","Cats") // Output: Cats dogs Cats
5
6
// You can use brackets [] to create a set of characters,
7
// any of which will be matched
8
replaceAll("Dogs dogs Dogs", "[Dd]ogs", "Cats") // Output: Cats Cats Cats
9
10
// You can also create a group with () and then use the | (OR) operator
11
replaceAll("Dogs dogs Dogs", "(D|d)ogs", "Cats") // Cats Cats Cats
12
13
// Accepts regex metacharacters, such as "\\b" which denotes "word boundary".
14
// Without \\b, this would output "Thwas was Sparta"
15
replaceAll("This is Sparta","\\bis\\b","was") // Output: This was Sparta
16
17
// replaceAll() is a great way to count elements in a string.
18
// Do this by using a regular expression to remove all characters
19
// except the commas that separate the elements (see the example
20
// database below for an in-depth look at this)
21
replaceAll("Dog, Cat, Monkey, Bat, Gorilla","[^,]","") // Output: ,,,,
22
// Apply length() + 1 to get the count!
This example database uses a multi-select property to track which superpowers each hero has. The Count formula uses
replaceAll()
to output a superpower count for each hero.
1
// Compressed
2
prop("Name") + " has " + format(length(replaceAll(prop("Powers"), "[^,]", "")) + 1) + ((length(replaceAll(prop("Powers"), "[^,]", "")) < 1) ? " power." : " powers.")
3
4
// Expanded
5
prop("Name") +
6
" has " +
7
format(
8
length(
9
replaceAll(
10
prop("Powers"),
11
"[^,]",
12
""
13
)
14
) + 1
15
) +
16
(
17
(
18
length(
19
replaceAll(
20
prop("Powers"),
21
"[^,]",
22
""
23
)
24
) < 1
25
) ?
26
" power." :
27
" powers."
28
)
When you call a multi-select property from within a formula, you end up with a single string where each select value is separated by a comma - e.g. “Super Strength, Ice Breath, Flight”.
As long as your multi-select options don’t contain commas themselves, we can take advantage of this fact. Since each value is separated by a comma, all we need to do is count the commas and then add one.
For example,
“Super Strength, Ice Breath, Flight”
has two commas. Adding one to that number gets us the total number of select options: three.But how can we count the commas? That’s where
replaceAll()
comes in.In our formula, we use
replaceAll(prop("Powers"), "[^,]", "")
to replace ALL instances of any character that isn’t a comma with ""
- an empty value. In effect, this removes all characters except the commas.- 1.The brackets
[]
define a set of characters where any of them will be part of the match. E.g. a regular expression like[Dd]og
would match both “Dog” and “dog”.- 1.Since we’re using
replaceAll()
instead of replace, using the brackets ensures that every character in our search string that matches a character in the brackets will be replaced.
- 2.The caret
^
used within the brackets[]
is a flag that says, “match a character that is not defined in the brackets”. - e.g.[^d]
would match any character that isn’t “d”. - 3.In the brackets, we define the comma
,
as the character after the caret.
In effect,
[^,]
says, “match any character that isn’t a comma”.This results in a string of commas - e.g. “Super Strength, Ice Breath, Flight” becomes “,,”.
From there, we can use the length function to get the length of our resulting string.
length(",,")
= 2
.Adding 1, we get the actual number of select tags in the row - in the case,
3
.The formula ends with another usage of this
replaceAll()
→ length()
chain within an if statement (using the conditional operators ?
and :
) in order to determine if the sentence should end with “power” or “powers”.
My name is Thomas Frank, and I'm a Notion-certified writer, YouTuber, and template creator. I've been using Notion since 2018 to organize my personal life and to run my business and YouTube channel. In addition to this formula reference, I've created a free Notion course for beginners and several productivity-focused Notion templates. If you'd like to connect, follow me on Twitter.

Last modified 4mo ago