MichaelMitchell
MichaelMitchell

Reputation: 1167

JS - having difficulties allowing whitespaces with regex

I am having problems allowing white spaces in js with regex. I am not sure what it is, but I have been searching and not able to find an answer that works.

I am really new to regex, so please be kind for being ignorant of something this simple.

Here is my allowed characters currently: var validChar = /^[A-Z0-9]+$/i; If I add a space in it, it will not work: /^[A-Z 0-9]+$/i

What do I have to do in order to make it recognize a space?

Upvotes: 1

Views: 88

Answers (2)

Buzz
Buzz

Reputation: 6330

Add white space in last,like this var validChar = /^[A-Z0-9\s]+$/i;

Upvotes: 1

Damask
Damask

Reputation: 1254

Space character in regexp is defined like "\s":

/^[A-Z0-9\s]+$/i

Upvotes: 6

Related Questions