regular expression to restrict number of consecutive characters

Discussion in 'Javascript' started by Sharkie, Oct 12, 2007.

  1. Sharkie

    Sharkie Guest

    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.
    Sharkie, Oct 12, 2007
    #1
    1. Advertising

  2. Sharkie

    Lee Guest

    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?


    --
    Lee, Oct 12, 2007
    #2
    1. Advertising

  3. 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
    Thomas 'PointedEars' Lahn, Oct 13, 2007
    #3
  4. Sharkie

    Evertjan. Guest

    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)
    Evertjan., Oct 13, 2007
    #4
  5. Sharkie

    Evertjan. Guest

    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)
    Evertjan., Oct 13, 2007
    #5
  6. In comp.lang.javascript message <>,
    Sat, 13 Oct 2007 00:35:23, Randy Webb <> 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)
    Dr J R Stockton, Oct 14, 2007
    #6
  7. Sharkie

    Sharkie Guest

    On Oct 13, 7:23 am, "Evertjan." <> 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.
    Sharkie, Oct 16, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,287
  2. Kannan S
    Replies:
    2
    Views:
    4,940
  3. Ken Fine
    Replies:
    2
    Views:
    196
    Ken Fine
    Feb 5, 2004
  4. Replies:
    12
    Views:
    1,932
    Dr.Ruud
    Nov 2, 2006
  5. Sharkie
    Replies:
    12
    Views:
    187
    Jambalaya
    Feb 29, 2008
Loading...

Share This Page