S
Sharkie
I need a regular expression which will evaluate to false if number of
consecutive characters (non-whitespace) exceeds certain number (10 in
this example).
For example, I have this function:
function test() {
var sValue="short unusuallyLongAndWayTooLongString short2";
var regEx=/\S{10,}/;
return regEx.test(sValue);
}
I will return true, because there is an instance of consecutive
characters longer than 10. I need it to return false.
Is there a way to "invert" a regular expression as part of the RegEx
(such as ^ in groups [^A-Z])? Before you tell me to invert the return
value:
return !regEx.test(sValue);
I can't do this, since this function is a generic validation function
which enforces bunch of rules on many occasions. I hardcoded both
value and RegEx here, but in production it will receive blindly both
value and RegEx and return evaluation result. If false, certain error
is reported, which is what I need.
I also tried inverting the numbers in curly brackets to limit
consecutive chars:
var regEx=/\S{,10}/;
but it doesn't work either since the RegEx will find word "short" and
"short2" and evaluate to true. For this to work all of the words would
have to be longer than 10 chars. I need it to return false as soon as
1 instance of too long of a string is found.
consecutive characters (non-whitespace) exceeds certain number (10 in
this example).
For example, I have this function:
function test() {
var sValue="short unusuallyLongAndWayTooLongString short2";
var regEx=/\S{10,}/;
return regEx.test(sValue);
}
I will return true, because there is an instance of consecutive
characters longer than 10. I need it to return false.
Is there a way to "invert" a regular expression as part of the RegEx
(such as ^ in groups [^A-Z])? Before you tell me to invert the return
value:
return !regEx.test(sValue);
I can't do this, since this function is a generic validation function
which enforces bunch of rules on many occasions. I hardcoded both
value and RegEx here, but in production it will receive blindly both
value and RegEx and return evaluation result. If false, certain error
is reported, which is what I need.
I also tried inverting the numbers in curly brackets to limit
consecutive chars:
var regEx=/\S{,10}/;
but it doesn't work either since the RegEx will find word "short" and
"short2" and evaluate to true. For this to work all of the words would
have to be longer than 10 chars. I need it to return false as soon as
1 instance of too long of a string is found.