• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's wrong in my regex comparison function ?

 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 656
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A test of match or no-match is simple: just use the .test() method of the regex.
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for enlighten me !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic