RegExpValidator MXML
Hi
I have a problem with a regular expression. I have an RegExpValidator in MXML and I want that is invalid when the source contain a or bMy RegExpValidator is<mx:RegExpValidator source="{value}"
property="text"
expression='.*[^ab].*'
valid="isValid(event)"
invalid="isInvalid(event)"/>
My expression is expression='.*[^ab].*'
When it's just a, b or a and b (one or many times) the expression is invalid : OK
Imagine the string abc
. If you apply the regex .*[^ab].*
to it, the first .*
will match ab
, the [^ab]
matches c
, and the final .*
matches the empty string.
Also, if you don't anchor your regex to the start and end of the string, it might happen (depending on the implementation of your validator) that the regex declares success if only a substring matches.
You want this:
^[^ab]*$
This matches any number of characters except a
or b
. ^
anchors the regex to the start, $
to the end of the string.
There are a lot of online tools that can help you with finding the right RegExp. Some of them might take you a while to perfect :P
The one I mostly use is this one: http://gskinner.com/RegExr/
Cheers
精彩评论