.match() in regular expressions returning extra parameters

Tell us what’s happening:

I see that the .match() method ( in the code below ) is returning -
[ ‘coding’,
index: 18,
input: ‘Extract the word ‘coding’ from this string.’,
groups: undefined ]

But as per the example given in the challenge, it must return only the first element in the array - can someone clarify?

Your code so far

let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36.

Challenge: Extract Matches

Link to the challenge:

Don’t count on FCC as your source for all the information about javascript you will ever need. I don’t know if you have or not, but I constantly find myself referring back to various resources for what functions expect for parameters, or what they will return. Print books, SitePoint’s library, MDN docs, or (my personal favorite) devdocs: https://devdocs.io/javascript/global_objects/string/match

If you read that last link, and specifically the section on ‘returned values’, it tells you something interesting (and franky, something I don’t think about too often). If you write the regex parameter like this: /coding/g, you will get an array of all the matches of that sub-string from within the original. IF, however, you use /coding/ without the g flag, you will get a single instance (it’s not searching globally, it only matches the first result), and that will contain more information about that single location in the string.

1 Like

Thanks, @snowmonkey - I have been facing similar challenges in other questions. I feel the moderator should take note and modify these questions and examples so that the learner is not confused.

But never the less this was very useful. Particularly thanks for the devdocs resource - I will start using it. :slight_smile: !

Have a great day.

the moderators do not have any special power in changing the curriculum. Everyone is able to submit an issue, or even a pull request, to request changes and suggestions of improvements.
Free Code Camp is open source and it is as it is thanks to the many many many volunteers that offer their help.


Technically example is correct as you will have only one enumerable property. It’s totally fine to ignore non-enumerable properties when depicting an array, as any kind of iteration would also ignore them:

let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/;
let result = extractStr.match(codingRegex);

// .forEach()
result.forEach((item) => console.log(item)); // coding

// .splice() and spread
result.slice(); // ['coding']
[...result]; // ['coding']

I need to learn how to raise a pull request, once I do,I will share my inputs

I am not sure I agree 100%, specially when it clearly says what will be returned. But I guess, I see a point in what you are saying - but its these inaccuracies that lead to confusion - IMHO

you can open an issue to raise your inputs, the pull request is to change the code

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

1 Like

The coursework at FCC, or any curriculum (online or in-person) is incomplete. Not saying it’s a bad or good thing, simply stating the fact.

Nearly every question I answer involves pulling an answer out of that devdocs link I’d sent - FCC is great for the foundations of the language, but looking at what parameters it can handle, or what values it might return, that’s a far deeper conversation and one you’ll need to research a LOT on your own.

Same can be said for a physical school, if it’s any good - simply assuming the coursework is giving you everything you’ll need is short-changing yourself. I use that devdocs (and about sixty other heavy-duty resources) each and every day. I am constantly looking up parameter order, or return types, or whether this function modifies this thing in place or in a clone… I can’t remember all that!

Coding, for me at least, is at least 2/3 research.

1 Like