Content Creators Contracting Project: Do I need to use regular expressions?

I am doing the Content Creators Contracting project for the backend api using JS course.

The project overview references regular expressions, and states that you are not supposed to know it yet. However, looking at the project It seems like that you need to use regular expressions, because what is inside the parentheses of the “filename.match” doesn’t seem like anything I know, and it looks similar to a regular expression.

line of code that is given:
“const extension = filename.match(/..([^.])$/)[1];”

I am pretty sure that I know what is supposed to be done, but I do not know how, or if I need to use regular expressions. This project has had me stumped for a while now.

Hi there, welcome to the forums!

Regular expressions (regex) are a pretty powerful tool, but they can be extremely confusing. For the Content Creators Contracting project, the regex is provided for you.

You don’t need to do anything with the expression, or even understand how it works.

Your task is to create a function called getContentType, which takes a parameter called filename.

function getContentType(filename) {

All you need to know is that this line:
const extension = filename.match(/.*\.([^\.]*)$/)[1];
will give you the file extension of whatever the filename was passed to your function.

As an example, let’s say I do this:

function getContentType(filename) {
    const extension = filename.match(/.*\.([^\.]*)$/)[1];

getContentType("example.pdf"); // output: pdf
getContentType("page.html"); // output: html
getContentType("essay.docx"); // output: docx

The regex gets the extension and puts it in the variable extension for you. All you need to do is write the rest of the code to use that extension to work out what the file type is - much like how you know that .pdf indicates a PDF document. :slight_smile:

You don’t need to worry about what the regex is doing, you just need to work with the variable extension. :slight_smile:

Thanks, I understand now.

1 Like

I have absolutely no idea how to begin this project and what to enter into my computer’s terminal. Is there a tutorial video on this?

Hi there, welcome to the forums!

I believe that if you click the “Get Help” link, there’s a video walkthrough of a Codecademy dev doing the project. :slight_smile: