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 Excluding Characters

mohamedmousa
mohamedmousa
289 Points

what is the answer for practice number 1 ??

1 ) Match:

foxes jumping dogs

Exclude:

aaaaa


my answer :

[^a]+

[^a]{3,7}

but both answer not match

6 Answers

Steven Parker
Steven Parker
231,236 Points

Your first answer ("[^a]+") seems to work. If I put that into regexpal all the match words are highlighted in yellow, but the exclude word is not.

Does it do something else for you?

mohamedmousa
mohamedmousa
289 Points

The same happens with me, all the match words are highlighted in yellow, but the excluded word is not.

Steven Parker
Steven Parker
231,236 Points

So doesn't that mean the answer is correct? Or does it need to be constrained to words? ("[^a\W]+" gives 3 separate matches)

Robert Reed
Robert Reed
2,529 Points

[^a]+ is not a correct answer.

As you all have mentioned, this highlights everything in yellow, indicating it is a single match, rather than 3 separate matches as is intended.

Of the solutions proposed, Nathan's solution of [d-x]+[^a] works (although the [^a] is not needed), and Steven's solution of [dfj]\w+ also works.

I wonder what the instructor's "ideal" solution is.

Gabbie Metheny
Gabbie Metheny
33,778 Points

I used [^a\s]+, which excludes the letter 'a' as well as any white space (spaces or new line characters between words), and requires 1 or more characters for a match. I got three separate matches for foxes, jumping and dogs.

Steven Parker
Steven Parker
231,236 Points

That works as long as there's no punctuation. But unlike "[^a\W]+", that will include any punctuation with the matched words.

Gabbie Metheny
Gabbie Metheny
33,778 Points

That's a good point, Steven. The example Joel gave didn't contain any punctuation, so I think either would work equally well in this specific case.

As Steven said, the correct answer is: ("[^a]+"). Everything is highlighted except ("aaaaa").

Nathan Brenner
Nathan Brenner
35,844 Points

/[d-x]+[^a]/ is another solution for this case.

Steven Parker
Steven Parker
231,236 Points

if you constrain the first letter, you don't really need to exclude the a's:
/[dfj]\w+/ would work too.

Keith McGill
seal-mask
.a{fill-rule:evenodd;}techdegree
Keith McGill
Full Stack JavaScript Techdegree Student 6,852 Points

The instructions did not explicitly say we must use a negated character set([^]) so I did: [b-z]{4,7} and got 3 separate matches and excluded the aaaaa.