Why the syntax difference on .test and .match?

I’m wondering if someone could explain, conceptually, why .test and .match have swapped syntax. For example:

/cat/.test("The big cat") searchTerm.test(myStr)
"The big cat".match(/cat/) myStr.match(searchTerm)

I think the short answer is because of how prototypes work.

match is a method on the String object String.prototype.match() it is called like other string methods on the string, for example 'SOMESTRING'.toLowerCase()

test is a method on the RegExp object RegExp.prototype.test() it is called on the regex and passed the string as an argument.

const str = 'table football';
const regex = new RegExp('foo*');
console.log(regex.test(str));

Edit: There is also some boxing/unboxing going on, which is sort of relevant as well I guess. Boxing is converting from primitive to object wrapper, and unboxing is converting from object wrapper to primitive.

You can read more in You Don’t Know JS: Types & Grammar


const string1 = 'literalString'
console.log(typeof string1); // string
console.log(string1 instanceof String); // false

const string2 = new String('constructedString')
console.log(typeof string2); // object
console.log(string2 instanceof String); // true

console.log(typeof /\s*/); // object
console.log(/\s*/ instanceof RegExp); // true

const regex = new RegExp(/\s*/);
console.log(typeof regex); // object
console.log(regex instanceof RegExp); // true
2 Likes

Thank you so much. I can see that there is lots I need to learn before I can really understand your answer. I’ll take a look at the links you gave.

Shorter answer:

This is a string: "hello". You want to do something directly with that string, so there are a set of useful functions for working directly with strings that you can use.

They are all of the form string.function. This looks like how you access objects, because it is. The string is the object and the function is the property of the object

This is a regex: /hello/. You want do something directly with that regex, so there are a set of useful functions for working directly with regex that you can use.

They are all of the form regex.function, the regex is the object and the function is the property of the object

But the way I look at it (which is why I asked) is that I don’t want to do something with the regex. I still think of wanting to do something with the string I am searching in. At least the distinction isn’t clear enough for me to automatically remember which is which when I go to write the thing in my code.

Thanks!

Luckily when you’re using a proper IDE with autocompletion as most professionals do, you won’t need to remember which one’s which. Unfortunately the freeCodeCamp editor does not help you in that regard.

1 Like

Have you tried using VisualStudioCode.
It is a very useful editor and can link to Github.

Each project/ program must be in it’s own folder. Then you Open Folder and create files and folders inside there. As you create a new file, it asks for the LANGUAGE. Click on the highlighted language text and pick html, css, javascript, python, or whatever.

Note:- You will need certain EXTENSIONS for certain languages. Plus you need the Live Server to get the ‘Go Live’ option on bottom left corner to see the web page you are designing.

1 Like

@colinthornton & @elswise Thanks! I am actually already using VS Code. And it is indeed a big help. I just thought there might be a way to understand it so I could remember on my own.

You really only need to remember one of them (process of elimination). If you can remember that match is a string method and is called on the string (just like other string methods) you know test is the one that gets the string passed as an argument and not called on it.

The methods are also used for different things so that is another hint to help you remember the syntax, test just returns true or false, match returns an array.

In the end, using them enough times is what will help you remember the difference.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.