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

using string.match()

Hey guys, working on a project and I don't know how to catch the + character using .match as every time it throws an error, I will include a code snippet, hopefully someone can help me out!

var regexPlus = ("+");

if (obj.name.length>0 && obj.number>0 && obj.number.match(regexPlus)) {

I get the error:

undefinedauth.js (line #91): Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat

And it caught integers fine using the same formatting in regexPlus so I was hoping that it would be fine on symbols too

1 Answer

(+) is a special character in regex. it means match 1 or more of the preceding character. to match the symbol itself it needs to be escaped. the escape character is (\). put a \ in front of the + and see if that works.

Sadly no luck, thanks for the idea though, it's a tricky fix it seems and I can't find much on the internet