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

Kyle Cameron
Kyle Cameron
20,683 Points

RegEx Challenge Help

I am attempting the coding challenge for this section and am stumped. I am using the following Regex, /^#([a-f0-9]{6})/i, and as far as I can tell it will only match a hexadecimal values. My problem is when I submit it fails because it also matches other values. I cant seem to figure out what it matches beyond a CSS Hexadecimal value to exclude those variables. Can anyone help?

2 Answers

Kyle Cameron
Kyle Cameron
20,683 Points

I figured this out on my own after reading your reply Stephen Parker. I remove the caret symbol and is still didn't work. After doing this it dawned on me that adding a $ at the end of the regex before the flag would make the regex inclusive, like the task instructions mentioned. I also removed the parenthesis. The final regex looks like this: /^#[a-f0-9]{6}$/i

Steven Parker
Steven Parker
231,141 Points

I think you misunderstood my answer, I was suggesting that you should anchor both ends, not remove the front one. I revised my wording a bit to clarify. Your final solution is exactly what I expected it should be.

Steven Parker
Steven Parker
231,141 Points

It would help to see the actual requirements (a link to the course page).

Bu one thing that stands out is this regex is anchored to the beginning (with "^"), but it's not anchored at the end and probably should be. As is, it would find the intended matches but also find them in strings having trailing characters.