Regular Expressions : .match()

Regular Expressions : .match()


In the freecodecamp quest, it says, if the regex you passed in got a match, the regex will be returned in a string and had an object wrapper on it.

I typed in these commands in chrome console, and the result I get is this:

let string = "hello world";
let regex = /hello/;
["hello", index: 0, input: "hello world", groups: undefined]

Where did all the extra info coming from???


According to MDN, that’s just what gets returned - although interestingly, when I try this in the terminal in Node, it doesn’t provide the groups: undefined part, so that must be a particular browser interpretation of the spec, I guess?

It’s quite useful to have this array, particularly for the index property, which you can use to locate your matched text in a string, eg:


Further reading: