Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > regular expression to restrict number of consecutive characters

Reply
Thread Tools

regular expression to restrict number of consecutive characters

 
 
Sharkie
Guest
Posts: n/a
 
      10-12-2007
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.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      10-12-2007
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?


--

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-13-2007
Sharkie wrote:
> 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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-13-2007
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.




> I need it to return false as soon as
> 1 instance of too long of a string is found.





--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-13-2007
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.


>> I will return true, because there is an instance of consecutive
>> characters longer than 10. I need it to return false.


Sorry, I see you allow 10 chars, try:

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



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-14-2007
In comp.lang.javascript message <(E-Mail Removed)>,
Sat, 13 Oct 2007 00:35:23, Randy Webb <(E-Mail Removed)> posted:
>Sharkie said the following on 10/12/2007 6:18 PM:
>> 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 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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
Sharkie
Guest
Posts: n/a
 
      10-16-2007
On Oct 13, 7:23 am, "Evertjan." <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression to test and limit number of characters Sharkie Javascript 12 02-29-2008 03:41 PM
regular expression consecutive numbers or letters mchesak@gmail.com Perl Misc 12 11-02-2006 08:49 AM
Regular expression for n consecutive characters Kannan S Java 2 06-02-2005 05:52 AM
Interrogating string for number of characters, response.writing identical number of characters on new line Ken Fine ASP General 2 02-05-2004 03:40 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments