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 Regular Expressions in JavaScript Regular Expressions Matching Specific Characters

I can not match #4 please help

My regex is :

/[lf]ad[yi][bn][ug]g?[ rug!]?/

it is supposed to match both these strings but does not match second one:

ladybug fading rug!

How to get the regex to also match : fading rug!

7 Answers

Nathan Brenner
Nathan Brenner
35,844 Points

Another possible solution: /[fl]adi?n?[gy] ?[br]ug!?/

As far as figuring out how to get from the first case to the second, I wrote the first case into the regex expression, then I added another test case and changed the regex.

For example, started with /ladybug/ and only testing for ladybug and fading rug!, ladybug passes but fading rug ! fails, so I added another test case fadybug and changed the regex to [fl]adybug. I ended up added 6 additional test cases. To go a step further, I would want to keep changing the regex so it only matches the first two cases.

Chirag Mehta
Chirag Mehta
15,332 Points
[fl]ad[iy]n?g? ?[br]ug!?

Here's my answer:

/[lf]adi?n?[gy][ ]?[br]ug!?/

Here's mine...

/[lf]ad[yi]b?[un]g ?r?u?g?!?/

my answer was

/[lf]adi?n?g?y? ?[br]ug!?/

bare in mind, only one character inside a square bracket will ever be used.

so if you have [rug!] like you have, it will only try to match either 'r', 'u' or 'g'. Never the whole string 'rug'

[lf]ad[yi][bn][ug][g ]r?u?g?!?