File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes What's wrong in my regex comparison function ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "What Watch "What New topic
Author

What's wrong in my regex comparison function ?

Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 174
good day,

I have a function which check whether the input is following the pattern as below



the above function good enough to detect input text like "abc123-12-566" with the pattern provided.
however, the problem occur when i try to introduce a space between the "-" , means if my input is "abc123 - 12 - 566" it can be accept.

I have try a few change and appreciated if someone can enlighten me what's wrong with my change :-

first change
============
var myPattern = /^[a-zA-Z0-9\\-\s]*$/; - it give me "invalid range in character class" error, anyone have idea ?

second change
==========
var myPattern = /^[a-zA-Z0-9\\- ]*$/; - added a space right after "-" , but same error with "Invalid range in character class"

I can't figure out how i can introduce a space into pattern for regular expression comparison.

Thanks in advance !
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
>var myPattern = /^[a-zA-Z0-9\\-]*$/;
I am not sure that is a very satisfactory regex patterm even for your original (no space) use - such as it matches empty string as well?

If you have precise info on the targetted data to match such as "abc123-12-566", it is better to use fully the info to strike a balance between general and special.

I can propose something like this.

If any worry you may have on multi-line flag, you can explicitly use \s an empty space no problem, like this.

If you think the space is not very visible, you can put it in a charset like this.

If you need to allow more than one space, just replace the ? by *.


Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 174
Thanks g tsuji,
beside of this, i also confuse on which is good validator/checker for javascript regex, after the pattern wrote, we expect to do a test with the test string, but i have no idea how i test it
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
A test of match or no-match is simple: just use the .test() method of the regex.
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 174
Many thanks for enlighten me !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's wrong in my regex comparison function ?
 
Similar Threads
regular expression
Quick Javascript & RegEx quuestion
Invalid RegEx on @Pattern
javascript validation
Recieving 'null' values from a jsp page in Struts2 Action