Javascript regex .test() "Uncaught TypeError: undefined is not a function"

javascript, jquery, regex

Solution

As it currently stands, `nameRegex` isn't a regex but a string and `String` doesn't have `test` functon which is why you are getting that error.

Remove the quotes around your regex. That is the literal form of regex.

var nameRegex = /^[a-zA-Z0-9_]{6,20}$/; //remove the quotes

Problem

Just trying to use javascript's regex capabilities with the `.test()` function. ``` var nameRegex = '/^[a-zA-Z0-9_]{6,20}$/'; if(nameRegex.test($('#username').val())) { ... } ``` The error is on this line `if(nameRegex.test($('#username').val())) {` Debugger breaks there and says "Uncaught TypeError: undefined is not a function". It seems like `.test()` is not defined? Shouldn't it be?

Original source