regular expression to restrict number of consecutive characters

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.
 
L

Lee

Sharkie said:
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.

Apparently it's not really a very "general" validation function,
if you can't at least specify the sense of the test. Is that
something that you can fix, or is this yet another request for
help coding around bad systems that can't be changed?


--
 
T

Thomas 'PointedEars' Lahn

Sharkie said:
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])?

Yes, you can "invert" the return value of RegExp.prototype.test().
Before you tell me to invert the return value:

return !regEx.test(sValue);

Too late.
I can't do this,

Yes, you can.
since this function is a generic validation function which enforces
bunch of rules on many occasions.

Why, you can do several tests on a string, invert one result and don't
invert the other(s), and combine the values with logical operators.
I don't see a problem here.
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.

Actually, it will only find `short', and then it returns `true'.
For this to work all of the words would have to be longer than 10 chars.

How do you define a word here?
I need it to return false as soon as instance of too long of a string is
found.

No, you need it to return `true' then, and then invert the return value.
There is no other *efficient* way. BTW, the opposite of 0 <= x <= 10 is
x > 10 (in integer: x >= 11), _not_ x >= 10.

!/\S{11,}/.test(sValue)


PointedEars
 
E

Evertjan.

Sharkie wrote on 13 okt 2007 in comp.lang.javascript:
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);
}

the comma would not be needed:

var regEx=/\S{10}/;

This tests for the first occurrence
of a string of 10 non-whitespace chars.
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.

This will work:

var regEx = /^\s*(\S{1,9}(\s+|$))*$/;

This tests for the complete absense of a
(non-whitespace char or end-of-string)
after 10 nonspace characters.
 
E

Evertjan.

Evertjan. wrote on 13 okt 2007 in comp.lang.javascript:
This will work:

var regEx = /^\s*(\S{1,9}(\s+|$))*$/;

This tests for the complete absense of a
(non-whitespace char or end-of-string)
after 10 nonspace characters.

Sorry, I see you allow 10 chars, try:

var regEx = /^\s*(\S{1,10}(\s+|$))*$/;
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Sharkie said the following on 10/12/2007 6:18 PM:

var limit=10;
var sTemp = sValue.split(' ');
var sValue="short unusuallyLongAndWayTooLongString short2";
var sTemp = sValue.split(' ');
for (var i=0;i<sTemp.length;i++)
{
if(sTemp.length>limit)
{
return false;
}
}
return true;
}

I see no regular expression in that. And I rather doubt whether it will
work as written.

The best way, though not in accord with the stated condition, is to
change the OP's return statement to
return ! regEx.test(sValue)
although if the answer MUST be given by a RegExp test the following
could be offered :
return /f/.test(regEx.test(sValue))

IMHO. the OP's generic validation function should be given an additional
argument which can be used to control inversion of its result. Allow
for the argument being absent, making that give what the function
currently gives. The argument should be true or false but 1 or 0 will
do.
 
S

Sharkie

var regEx = /^\s*(\S{1,10}(\s+|$))*$/;

Thank you all for the excellent advice, and Evertjan:
your solution does EXACTLY what I need - thanks!
No customization, additional parameters, and special cases
needed. I hear your logic, and was trying to do something
similar, but could not get the syntax right. Well done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top