# test

Learn how to use the test function in Notion formulas.
The `test()` function allows you to test whether a string contains a substring, the latter of which can be a regular expression. If it does, the function returns true.
1
test(string, string [regex supported])
`test()` accepts two string arguments and outputs a Boolean result.
Good to know: `test()`, replace, and replaceAll are able to automatically convert numbers and Booleans (but not dates) to strings. Manual type conversion is not needed.

## Example Formulas

1
test("Monkey D. Luffy", "Luffy") // Output: true
2
3
// test() is case-sensitive
4
test("Monkey D. Luffy", "luffy") // Output: false
5
6
// You can use brackets [] to create a set of characters,
7
// any of which will be matched
8
test("Monkey D. luffy", "[Ll]uffy") // Output: true
9
10
// You can also create a group with () and then use the | (OR) operator
11
test("Monkey D. luffy", "(L|l)uffy") // Output: true
Check out the regular expressions page to see a lot more that you can accomplish using them!

## Example Database

This example database contains some file attachments. The Meta formula property displays information about each file, including its media type and hosting location (within Notion or hosted externally).
Good to know: This exact example is also used for the contains function. I recommend checking them both out to see how `test()` is a much more flexible tool.,

### “Meta” Property Formula

1
// Compressed
2
if(test(prop("File"),"([jJ][pP][eE]?[gG]|[gG][iI][fF]|[pP][nN][gG])"),"🌅 Image",if(test(prop("File"),"([mM][pP]3|[wW][aA][vV]|[aA][iI][fF]{2})"),"🎧 Audio","📝 Text")) + "\n" + if(test(prop("File"),"secure.notion-static.com"),"✅ Internal","⛔️ Externally Hosted")
3
4
// Expanded
5
if(
6
test(
7
prop("File"),
8
"([jJ][pP][eE]?[gG]|[gG][iI][fF]|[pP][nN][gG])"
9
),
10
"🌅 Image",
11
if(
12
test(
13
prop("File"),
14
"([mM][pP]3|[wW][aA][vV]|[aA][iI][fF]{2})"
15
),
16
"🎧 Audio",
17
"📝 Text"
18
)
19
) +
20
"\n" +
21
if(
22
test(
23
prop("File"),
24
"secure.notion-static.com"
25
),
26
"✅ Internal",
27
"⛔️ Externally Hosted"
28
)
`test()` is essentially the same as the contains function, except it allows for the use of regular expressions.
This makes it far more powerful, and allows you to do things like case-insensitive matching. You can also include optional characters that don’t have to be included in the match.
Good to know: Regular expressions are incredibly powerful, but have a steep learning curve. Check out the regular expressions page for a primer on how to use them in Notion formulas, as well as resources for learning them more deeply.
This formula replicates the functionality of the example formula in the contains() function example database, but it uses `test()`'s regular expression capability to reduce the size of the formula and add case-insensitivity.
If you want to understand this formula fully, you should read the regular expressions page as well as the page on if statements.
But let’s break apart this small chunk of the formula here:
1
test(
2
prop("File"),
3
"([jJ][pP][eE]?[gG]|[gG][iI][fF]|[pP][nN][gG])"
4
)
`test()` returns true if the first string argument contains the substring specified in the second argument.
Here, our second argument is a regular expression. It is essentially saying:
Match “jpg”, “jpeg”, “gif”, or “png” - regardless of character case.
How is it doing that? Let’s break it down.
• The parentheses `()` form what’s called a capture group.
• The pipe symbols `|` act as Boolean OR operators
• The brackets `[]` essentially say, “Match any single character that is included here”.
To make all of this easier to understand, let's start with the simplest version of a regular expression – a normal string.
1
"jpg"
Here, `test()` would only return true if the first argument’s string contains “jpg”.
But what if we want to match one of several options? We'll need to use the OR operators `|` for that, and they only work inside of a capture group created with parentheses `()`.
1
"(jpg|gif|png)"
Now, our `test()` function would return true if the first argument contains any of the following:
• jpg
• gif
• png
However, currently this would only happen if the substring was all lower-case. The substring “JPG” would not cause `test()` to return true.
To make our regular expression case-insensitive, we’ll define a character class using brackets `[]`.
Here’s a very simple class:
1
"[Jj]"
This will create a match if the first argument’s string contains “j” or “J”.
To make our entire set of potential file extensions case-insensitive, we simply need to define a similar character class for each letter:
1
"[Jj][Pp][Gg]|[Gg][Ii][Ff]|[Pp][Nn][Gg]"
This regular expression will create a match for any of our three file extensions, regardless of the case (upper or lower) of any letter.
Good to know: Many programming languages and regex engines (such as JavaScript's) support flags that you can set to more easily do things like marking your search as case-insensitive.
Unfortunately, in our testing we've found that Notion seems to support none of these flags. This is why we have to do things "the hard way" and define a character class for every letter that we want to be case-insensitive.
Finally, let’s deal with the fact that “jpg” is sometimes spelled “jpeg”. We could simply add another “or” case to our regular expression, but there’s a more efficient way to do it.
By adding a `?` character after another character (or character class), we can tell the regular expression engine (or “regex engine”) that character is optional. In other words, we’ll get a match if it shows up zero or one times.
To keep things simple, here’s how we’d match for `jpg` or `jpeg`, with case-sensitivity:
1
"jpe?g"
Good to know: Since `?` is a special character, you need to “escape” it with a `\` if you actually want to match for a question mark – e.g. `\?`.
Now that you have that down, let’s add our `?` to the original regular expression:
1
"[Jj][Pp][Ee]?[Gg]|[Gg][Ii][Ff]|[Pp][Nn][Gg]"
With `[Ee]?` included, we’ll now match any version of “jpg” or “jpeg” regardless of any character’s case.

#### Making This Example Work in the Real World

This example deliberately keeps things simple in order to make the functionality of `test()` easy to understand.
If you actually wanted to determine the media type of a file attachment, you’d first want to extract the file extension from its URL (otherwise, any other instance of letters such as “jpg” or “mov” might cause your formula to mis-identify the file type).
In the example database above, I’ve also included a File Extension property with the following formula:
1
replace(prop("File"), ".*\\.(\\w+)\$", "\$1")
This uses the replace function along with another regular expression to replace the entire file URL with its file extension.
Want to understand how this regular expression works? Check out the replace function's example database; I've explained it there.
To make this example error-free, you’d ultimately want to have the Meta property target the output of File Extension, rather than the file’s URL directly. Alternatively, you could combine these two formulas. I’ll leave that as an exercise for the reader.