What's the difference in .match and .test?

You read the title and know it, I find them super similar in their usecases.

not really
match is a string method
test is a regex method

match returns an array
test returns a boolean

match is used to extract the matched pattern
test check if the pattern is in the string or not

2 Likes

Hey there,

Regex.test(string) returns a boolean (true/false) value if the string contains a match for the regular expression.

/hello/.test("hello world") returns true.

String.match(regex) (note that this one is backwards) returns an array of matches.

"Hello hello hELLO".match(/hello/i)returns ["Hello", "hello", "hELLO"].

2 Likes

Thanks @nhcarrigan and @ilenia

2 Likes