Reputation: 81
So, i been getting this kind of output lately when im coding but i just want to make sure its normal or maybe im doing something wrong. Here is a simple code.. maybe it has to do with regex.
my console says " (1) ['a', index: 1, input: 'karina', groups: undefined] "
function reg(s) {
reg = /[aeiou]/;
console.log(s.match(reg));
}
reg("turtle");
Upvotes: 0
Views: 98
Reputation: 60
Your code is working just fine. The .match() method will compare the string and the RegEx that you defined and return an array with the first match it finds and at what index it happens.
If you want to get back an array with all of the results and without the other information, all you need to do is add a "g" at the end of your RegEx. Your function should look like this:
function reg(s) {
reg = /[aeiou]/g;
console.log(s.match(reg));
}
reg('turtle');
The "g" at the end, will make so that .match() will look and grab all of the occurrences in the string that you are checking, instead of just the first one.
Upvotes: 1