The contains()
function tests whether the first argument contains the second argument. It only accepts strings (or nested functions that output strings).
contains(string, string)
string.contains(string)
Code language: JavaScript (javascript)
contains()
tests for the entire string passed via the second argument, is case-sensitive, and does not accept regular expressions.
Good to know: contains
will accept non-string data types for both the first and second argument. They will be converted to strings before comparing.
Good to know: The test function can do everything contains()
does and much more, including accepting regular expressions and testing non-string data types.
Example Formula
contains("Monkey D. Luffy", "Luffy") /* Output: true */
"Monkey D. Luffy".contains("keyLuf") /* Output: false */
contains(true, "true") /* Output: true */
contains("There are 10 members.", 10) /* Output: true */
Code language: JavaScript (javascript)
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).
View and Duplicate Database
“Meta” Property Formula
if(
contains(
prop("File"),
"jpg"
) or contains(
prop("File"),
"png"
) or contains(
prop("File"),
"gif"
) or contains(
prop("File"),
"jpeg"
),
"🌅 Image",
if(
contains(
prop("File"),
"mp3"
) or contains(
prop("File"),
"wav"
) or contains(
prop("File"),
"aiff"
),
"🎧 Audio",
"📝 Text"
)
) +
"\n" +
if(
contains(
prop("File"),
"secure.notion-static.com"
),
"✅ Internal",
"⛔️ Externally Hosted"
)
Code language: JavaScript (javascript)
File & Media-type properties return their file path as a string when referenced in Notion formulas.
This formula uses contains()
to check each file’s path for the presence of several common file extensions, such as jpg
, gif
, mp3
, etc.
It uses a nested if statement , as well as the or operator, to check for several different file extensions. It then outputs the file’s media type based on its match (or lack thereof).
Another if statement checks for the presence of the string secure.notion-static.com
, which is always part of the URL for files that are uploaded directly to Notion’s Amazon AWS locker.
Note: One major weakness of contains()
is that it can’t do case-insensitive matching. In this formula, we’d have to add many more “or” clauses in order to test for every case variation of each file type. Check out the test function’s example database to see a much more efficient way to achieve this exact same goal.