Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Regular Expressions in JavaScript Validating a Form Using Regular Expressions in JavaScript

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Regular Expressions test() - match()? Are these used interchangeably?

Just a quick question to help clear up some confusion.

Joel mentions the match() method but seems to use the test() method in its place. Do these 2 words means the same method? Are they used interchangeably? :) thanks

1 Answer

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

The match() method retrieves the matches when matching a string against a regular expression.

Use .test if you want a faster boolean check. Use .match to retrieve all matches when using the g global flag.

Enjoy!

REFERENCES http://devdocs.io/javascript/global_objects/string/match <-- match() http://devdocs.io/javascript/global_objects/regexp/test <-- test() https://stackoverflow.com/questions/10940137/regex-test-v-s-string-match-to-know-if-a-string-matches-a-regular-expression

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

I appreciate the response, thank you!. :-)

The question was more about why we don't see an example of match() in the video. Unless we did and I somehow missed it!