Regular Expression in Javascript

Psych

Senior member
Feb 3, 2004
324
0
0
I'd like some help with the RegExp object and literal in Javascript. I've read the entire Javascript 1.5 documentation in hopes of figuring out how to use regular expressions, but it doesn't seem to work.

Basically, all I want to do is to check a string to see if it is just a string of spaces and to reject it if it is. But it is rejecting the asterisk * in the regular expression. How am I supposed to do this???
 

cyberia

Platinum Member
Oct 22, 1999
2,535
0
0
Originally posted by: Psych
Basically, all I want to do is to check a string to see if it is just a string of spaces and to reject it if it is. But it is rejecting the asterisk * in the regular expression. How am I supposed to do this???
Are you trying to use /\b*/ to mean a string of spaces?

Check this out:
\s Matches a single white space character, including space, tab, form feed, line feed. Equivalent to [ \f\n\r\t\u00A0\u2028\u2029].

\b Matches a word boundary, such as a space.
 

cyberia

Platinum Member
Oct 22, 1999
2,535
0
0
Also, if you are just trying to see if the input is an integer, /^\d+$/ (for non-negatives) or /^-?\d+$/ (for all) should work.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
For a string of spaces, you probably want /^\s+$/ (one or more spaces).
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: cyberia
Also, if you are just trying to see if the input is an integer, /^\d+$/ (for non-negatives) or /^-?\d+$/ (for all) should work.

But he's not, he's trying to see if it's a string of spaces. :confused:
 

Psych

Senior member
Feb 3, 2004
324
0
0
Oh, I thought word boundary meant a character that delimits words, like spaces. Thanks for the help.
 

cyberia

Platinum Member
Oct 22, 1999
2,535
0
0
Originally posted by: notfred
Originally posted by: cyberia
Also, if you are just trying to see if the input is an integer, /^\d+$/ (for non-negatives) or /^-?\d+$/ (for all) should work.

But he's not, he's trying to see if it's a string of spaces. :confused:

I meant ultimately. I had given him a suggestion regarding a string of spaces, and MrChad went the extra mile when he gave his specific solution. But I also tried to guess Psych's ultimate intention.
 

Psych

Senior member
Feb 3, 2004
324
0
0
Didn't know that spaces could be literally used. Any way, I also have another query concerning this little snippet of code. The num variable inside of the function is ALWAYS given as a string from the form input box. Is there a particular reason why?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
The web doesn't really lend itself to strong typing, hence "everything is a string."

(of course, converting it to an integer type is no doubt trivial to do)
 

DJFuji

Diamond Member
Oct 18, 1999
3,643
1
76
for regex development in the future, you may want to check out a microsoft tool that helps out a LOT. It's called RegEx workbench. I've found it very helpful. Do a search on it with google and you'll find a few places to download. It's by Eric Gunerson i believe.
 

cyberia

Platinum Member
Oct 22, 1999
2,535
0
0
Wow, that Regex Workbench is very useful. However, I played with a regular expression that I know works well in one of my apps, and it wasn't finding matches where it should have. I wasn't using it correctly I guess.

Does anybody know of other useful RegEx tools?
 

DJFuji

Diamond Member
Oct 18, 1999
3,643
1
76
Originally posted by: notfred
Originally posted by: cyberia
Does anybody know of other useful RegEx tools?

This book.

RegEx workbench works the best in my experience, especially when writing your own expressions. I've used it very successfully in writing criteria to validate textboxes in ASP.NET web forms. I'm not aware of any other really good regex programs out there, though. VS.NET has a bunch of them built in, but doesnt really help you write your own.