Regular Expression to match any spaces

K

KDawg44

Hi,

Normally I can write regular expressions decently well but for some
reason I am having trouble getting this to work. I am validating form
data and need to throw an error if there are ANY spaces in the field.
abc123 is fine, abc 123 is not. Any character is fine, just not a
space.

Thanks for any advice.
 
T

Thomas 'PointedEars' Lahn

KDawg44 said:
Normally I can write regular expressions decently well but for some
reason I am having trouble getting this to work. I am validating form
data and need to throw an error if there are ANY spaces in the field.
abc123 is fine, abc 123 is not. Any character is fine, just not a
space.

/ /.test(s)

Or more generally, for all whitespace (including tabs, newlines etc.):

/\s/.test(s)

Either expression yields `true' if there is such a character in the argument
string `s', `false' if there is not.

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Tue,
/ /.test(s)

/\x20/.test(s) may be better. The meaning seems identical, but
the latter form seems less likely to be damaged without being noticed by
a human editing error. Non-human editors will see no possible problem.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted:
/ /.test(s)

/\x20/.test(s) may be better. The meaning seems identical, but
the latter form seems less likely to be damaged without being noticed by
a human editing error. Non-human editors will see no possible problem.

/[ ]/.test(s)

is another solution that requires a little bit less parsing to avoid that
unlikely problem. (Duplicate spaces will not add a new character to the
character class.)


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top