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
Noah Schade
17,694 PointsI'm trying to store the names starting with a capital 'S' into an array and log it out to the console.
I would like to do it using a regular expression. Here is my code that does not work.
const names = ['Selma', 'Ted', 'Mike', 'Sam', 'Sharon', 'Marvin'];
const sNames = [];
names.forEach(name => {
if(name === /^S\w*$/){
sNames.push(name);
}
});
console.log(sNames);
// Result should be: ['Selma', 'Sam', 'Sharon'];
2 Answers
Bhavesh Hirani
7,381 PointsThe Regex pattern should be ^S\w and use the match function instead of ===.
if(name.match(/^S\w/)){
sNames.push(name);
}
Noah Schade
17,694 PointsThank you for your help Bhavesh!