Little confused with the wording

In the first lesson of regex’s: Here

It shows this:

Which i find rather confusing in the wording.
The .test() method takes the regex
It looks like its taking a string, not a regex?

applies it to a string (which is placed inside the parentheses)
What’s already is in the parentheses is already a string?

Am i right to assume they mixed these up?
I believe from the looks, the .test() takes a string, and tests it against it’s regex’s value.

You are correct. The test method used on a Regular Expression data type accepts a string as its argument.

EDIT: I do not thin k the sentence necessarily needs to be changed because it is basically saying what I just explained. Maybe a better wording would be:

The test method applies the regex to a string passed to it (placed inside the parentheses),

1 Like

As you might know already, there are functions and there are methods. Methods are functions which are declared on an object. To call the method, you access the object.
For example

const someObj = {
  method(string) {

// logs "test"

The fun part is, strings and regex and generally most other stuff in JS have their methods. You alreayd know arrays and the planty of methods they got. Any array you define have access to those methods, all you need to do is call it on the array itself. For example [1, 3, 2].sort(). An example of a regex method is test. You just call it on any regex. test takes a string as a parameter, which is used to be tested against the regex. /regex/.test("string"). A confusen can arrise with match, which is a string method, i.e. you call it on a string. It also takes a regex as an argument. So it looks like this - "string".match(/regex/). Lot of people(me included) get confused in the start, why the string/regex places are reversed, or why match and test work in opposite ways. It is because one is method defined on the String object, while the other is a method belonging to the RegExp one. They also happen to take as argument the opposite, which makes for a confusing banther. And just like match, there are other string methods, like replace, slice, toUpperCase, which also happen to take quite different arguments(or none), and slice is also a method on the Array object(another fact that can confuse newbie).

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