• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Regular Expression in Javascript

Psych

Senior member
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???
 
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.
 
Also, if you are just trying to see if the input is an integer, /^\d+$/ (for non-negatives) or /^-?\d+$/ (for all) should work.
 
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. 😕
 
Oh, I thought word boundary meant a character that delimits words, like spaces. Thanks for the help.
 
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. 😕

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.
 
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?
 
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)
 
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.
 
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?
 
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.
 
Back
Top