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

What's the answer to practice no.2 in regex excluding characters?

We need to match these

34522
72379

& exclude these:

1234k
5784k
5784k

5 Answers

Steven Parker
Steven Parker
231,124 Points

In regex, there's nearly always multiple ways to perform a task. And here, much of the job is defining exactly what the task is.

But one solution is to define this particular task as identifying "any number of digits that end on a word boundary", which would be:

\d+\b

Ah, I see. That \b is new to me...

Thank you.

Steven Parker
Steven Parker
231,124 Points

I wasn't sure what had been introduced in the course. Another way might be "digits that occupy an entire line":

^\d+$

What about \d+[^\dk] ?

Steven Parker
Steven Parker
231,124 Points

Why not try it in regexpal (or RegEx Pal) and see for yourself?

As the question suggests, the lesson introduces us to the [^] method of excluding characters and offers these questions to practice. It also used the inverse tokens \D, \W, \S.

So far the closest I can get does use the exclusion token but unfortunately still uses another token not yet provided:

\w+[^k]$

From research, I found that the $ token denotes the end of the test string.

  • If anyone manages this one with the exclusion or inverse token and only tokens introduced to this point, please do add your solution. A reminder so far videos have shown: [abc], ?, *, +, \d, \w, \s, . , {3}, {3, }, {3,5}

EDIT

Ah! I think I've got it....

[^\D]{5}

Technically I think you'd use Sam Munter's solution or Steven Parker's as this says "not, not a digit" but at least this now uses what has been shown in the videos.