This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 :-
var myPattern = /^[a-zA-Z0-9\\-\s]*$/; - it give me "invalid range in character class" error, anyone have idea ?
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.
>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 *.
Joined: Jul 24, 2011
Thanks g tsuji,
Joined: Jan 18, 2011
A test of match or no-match is simple: just use the .test() method of the regex.