Simple Regular Expression Help

Discussion in 'Javascript' started by Joe [at] subprint.com, Nov 24, 2008.

  1. What I want to do is the following:

    Test if a string matches any of the following:

    8,
    9,
    37,
    38,
    39,
    40,
    46

    I have this for my Regex literal:

    var allowed = /8|9|37|38|39|40|46/;

    But this fails.

    Help?
    Joe [at] subprint.com, Nov 24, 2008
    #1
    1. Advertising

  2. "Joe [at] subprint.com" <> writes:

    > What I want to do is the following:
    >
    > Test if a string matches any of the following:


    What do you mean by "matches"? Must it be equal to one of the
    following, or must it contain one of the following?

    > 8,
    > 9,
    > 37,
    > 38,
    > 39,
    > 40,
    > 46


    > I have this for my Regex literal:
    >
    > var allowed = /8|9|37|38|39|40|46/;
    >
    > But this fails.


    *How* does it fail?
    Does it fail to match something you wanted matched? (Unlikely, it
    looks fine for that).
    Does it match something tht it shouldn't? Likely, it isn't anchored,
    so it matches if any of the numbers are substrings of the string.

    Try:
    var allowed = /^(?:8|9|37|38|39|40|46|8|9)$/;

    If this doesn't work, you need to be more precise about how it fails,
    and what it is supposed to match and not match.

    /L
    --
    Lasse Reichstein Holst Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Nov 24, 2008
    #2
    1. Advertising

  3. On Nov 24, 4:40 pm, Lasse Reichstein Nielsen <>
    wrote:
    > "Joe [at] subprint.com" <> writes:
    >
    > > What I want to do is the following:

    >
    > > Test if a string matches any of the following:

    >
    > What do you mean by "matches"? Must it be equal to one of the
    > following, or must it contain one of the following?
    >
    > > 8,
    > > 9,
    > > 37,
    > > 38,
    > > 39,
    > > 40,
    > > 46
    > > I have this for my Regex literal:

    >
    > > var allowed = /8|9|37|38|39|40|46/;

    >
    > > But this fails.

    >
    > *How* does it fail?
    > Does it fail to match something you wanted matched? (Unlikely, it
    > looks fine for that).
    > Does it match something tht it shouldn't? Likely, it isn't anchored,
    > so it matches if any of the numbers are substrings of the string.
    >
    > Try:
    >  var allowed = /^(?:8|9|37|38|39|40|46|8|9)$/;
    >
    > If this doesn't work, you need to be more precise about how it fails,
    > and what it is supposed to match and not match.
    >
    > /L
    > --
    > Lasse Reichstein Holst Nielsen
    >  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    >   'Faith without judgement merely degrades the spirit divine.'


    It must be the EXACT MATCH, such as the value "8" or "9" but not "89"
    or "98"...Each vertical bar resembles an exact match, for a total of 7
    matches: 8,9,37,38,39,40,46
    Joe [at] subprint.com, Nov 25, 2008
    #3
  4. "Joe [at] subprint.com" <> writes:

    > On Nov 24, 4:40 pm, Lasse Reichstein Nielsen <>
    > wrote:


    >> Try:
    >>  var allowed = /^(?:8|9|37|38|39|40|46|8|9)$/;


    > It must be the EXACT MATCH, such as the value "8" or "9" but not "89"
    > or "98"...Each vertical bar resembles an exact match, for a total of 7
    > matches: 8,9,37,38,39,40,46


    In that case, the suggested regexp above is exactly right (ahem, if
    you ignore that 8 and 9 occour twice :)
    A slightly shorter form:
    var allowed = /^(?:[89]|3[789]|4[06])$/;

    /L
    --
    Lasse Reichstein Holst Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Nov 25, 2008
    #4
  5. In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
    6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]
    subprint.com" <> posted:
    >> --
    >> Lasse


    Please read the newsgroup FAQ more carefully.

    >It must be the EXACT MATCH, such as the value "8" or "9" but not "89"
    >or "98"...Each vertical bar resembles an exact match, for a total of 7
    >matches: 8,9,37,38,39,40,46


    Is it *necessary* that you use a RegExp method? Is this coursework, set
    to you or as self-tuition?

    Otherwise, to select a good solution, one needs to know whether the list
    of numbers is unvarying and so can be constructed by the author, or is
    variable and constructed by code.

    Also, is the test to be done once or many times? If the latter, it
    might be worth investing in set-up code to make the testing faster.

    Is anything known about the strings to be tested? If either there can
    be no leading zero or leading zero can be ignored, then they can be
    treated as if they were Numbers.

    Is the number of allowed values typically about 7? Can it be much
    larger?

    If constructed by code, consider
    // set-up :
    var Arr = []
    // here construct each number N in turn & do Arr[N] = true
    // test number J :
    if (A[J]) DoFoundCase() ; else DoNotFoundCase()

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Nov 26, 2008
    #5
  6. On Nov 26, 7:11 am, Dr J R Stockton <> wrote:
    > In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
    > 6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]
    > subprint.com" <> posted:
    >
    > >> --
    > >> Lasse

    >
    > Please read the newsgroup FAQ more carefully.
    >
    > >It must be the EXACT MATCH, such as the value "8" or "9" but not "89"
    > >or "98"...Each vertical bar resembles an exact match, for a total of 7
    > >matches:  8,9,37,38,39,40,46

    >
    > Is it *necessary* that you use a RegExp method?  Is this coursework, set
    > to you or as self-tuition?
    >
    > Otherwise, to select a good solution, one needs to know whether the list
    > of numbers is unvarying and so can be constructed by the author, or is
    > variable and constructed by code.
    >
    > Also, is the test to be done once or many times?  If the latter, it
    > might be worth investing in set-up code to make the testing faster.
    >
    > Is anything known about the strings to be tested?  If either there can
    > be no leading zero or leading zero can be ignored, then they can be
    > treated as if they were Numbers.
    >
    > Is the number of allowed values typically about 7?  Can it be much
    > larger?
    >
    > If constructed by code, consider
    > // set-up :
    >   var Arr = []
    >   // here construct each number N in turn & do   Arr[N] = true
    > // test number J :
    >   if (A[J]) DoFoundCase() ; else DoNotFoundCase()
    >
    > It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.
    >
    > --
    >  (c) John Stockton, nr London UK.   ?     IE7 FF2 Op9 Sf3
    >  news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    >  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    >  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


    With all due respect, Dr. Stockton, I'm a professional in the UI
    Development industry leading a team of UI Engineers, however, that
    does not make me an EXPERT with regular expressions. Of course I
    could have created an array and created two separate functions; this
    appears to be SLOW compared to simply testing a properly created
    regular expression.

    Moreover, my regex string, I felt, was correct, yet the code was not
    working as intended, so I posted it on this group in hopes of getting
    some answers. If you would like, here is my code:

    // Joe McCann's digits only jQuery plugin.
    (function($j){
    $j.fn.extend({
    pressdigits: function(){
    return this.each(function(){
    var obj = $j(this);
    obj.keypress(function(e){

    var unicode = e.charCode ? e.charCode : e.keyCode;

    // Integrate this instead of switch statement.
    var allowed = /^(?:8|9|37|38|39|40|46)$/;

    switch (unicode) {
    case 8:
    return true;
    break;
    case 9:
    return true;
    break;
    case 37:
    return true;
    break;
    case 38:
    return true;
    break;
    case 39:
    return true;
    break;
    case 40:
    return true;
    break;
    case 46:
    return true;
    break;
    }
    if (unicode < 48 || unicode > 57)
    return false;
    });
    });
    }
    });
    })(jQuery);


    It is a jQuery plugin only allowing digits as keyboard inputs.
    However, it currently uses a switch statement to allow for special
    keys such as tab and backspace, etc. since I was unable to get the
    regex to work properly.

    Also, the FAQ page gave me ZERO insight into my problem, but thanks
    anyway, Dr.

    Cheers.

    Joe
    Joe [at] subprint.com, Dec 1, 2008
    #6
  7. On Dec 1, 10:33 am, "Joe [at] subprint.com" <>
    wrote:
    > On Nov 26, 7:11 am, Dr J R Stockton <> wrote:
    >
    >
    >
    > > In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
    > > 6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]
    > > subprint.com" <> posted:

    >
    > > >> --
    > > >> Lasse

    >
    > > Please read the newsgroup FAQ more carefully.

    >
    > > >It must be the EXACT MATCH, such as the value "8" or "9" but not "89"
    > > >or "98"...Each vertical bar resembles an exact match, for a total of 7
    > > >matches:  8,9,37,38,39,40,46

    >
    > > Is it *necessary* that you use a RegExp method?  Is this coursework, set
    > > to you or as self-tuition?

    >
    > > Otherwise, to select a good solution, one needs to know whether the list
    > > of numbers is unvarying and so can be constructed by the author, or is
    > > variable and constructed by code.

    >
    > > Also, is the test to be done once or many times?  If the latter, it
    > > might be worth investing in set-up code to make the testing faster.

    >
    > > Is anything known about the strings to be tested?  If either there can
    > > be no leading zero or leading zero can be ignored, then they can be
    > > treated as if they were Numbers.

    >
    > > Is the number of allowed values typically about 7?  Can it be much
    > > larger?

    >
    > > If constructed by code, consider
    > > // set-up :
    > >   var Arr = []
    > >   // here construct each number N in turn & do   Arr[N] = true
    > > // test number J :
    > >   if (A[J]) DoFoundCase() ; else DoNotFoundCase()

    >
    > > It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.

    >
    > > --
    > >  (c) John Stockton, nr London UK.   ?    IE7 FF2 Op9 Sf3
    > >  news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    > >  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > >  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

    >
    > With all due respect, Dr. Stockton, I'm a professional in the UI
    > Development industry leading a team of UI Engineers, however, that
    > does not make me an EXPERT with regular expressions.  Of course I
    > could have created an array and created two separate functions; this
    > appears to be SLOW compared to simply testing a properly created
    > regular expression.
    >
    > Moreover, my regex string, I felt, was correct, yet the code was not
    > working as intended, so I posted it on this group in hopes of getting
    > some answers.  If you would like, here is my code:
    >
    > // Joe McCann's digits only jQuery plugin.
    > (function($j){
    >     $j.fn.extend({
    >         pressdigits: function(){
    >             return this.each(function(){
    >                 var obj = $j(this);
    >                 obj.keypress(function(e){
    >
    >                         var unicode = e.charCode ? e.charCode : e.keyCode;
    >
    >                                         // Integrate this instead of switch statement.
    >                                         var allowed = /^(?:8|9|37|38|39|40|46)$/;
    >
    >                     switch (unicode) {
    >                         case 8:
    >                             return true;
    >                             break;
    >                         case 9:
    >                             return true;
    >                             break;
    >                         case 37:
    >                             return true;
    >                             break;
    >                         case 38:
    >                             return true;
    >                             break;
    >                         case 39:
    >                             return true;
    >                             break;
    >                         case 40:
    >                             return true;
    >                             break;
    >                         case 46:
    >                             return true;
    >                             break;
    >                     }
    >                     if (unicode < 48 || unicode > 57)
    >                         return false;
    >                 });
    >             });
    >         }
    >     });
    >
    > })(jQuery);
    >
    > It is a jQuery plugin only allowing digits as keyboard inputs.
    > However, it currently uses a switch statement to allow for special
    > keys such as tab and backspace, etc. since I was unable to get the
    > regex to work properly.
    >
    > Also, the FAQ page gave me ZERO insight into my problem, but thanks
    > anyway, Dr.
    >
    > Cheers.
    >
    > Joe


    Using SpiderMonkey and jslibs:

    LoadModule('jsstd') // Gives us Print().

    function good_input( c )
    { if ( c.toString().search(/^8|9|37|38|39|40|46$/) >= 0 )
    return true
    return (c > 47 && c < 58)
    }

    for (var i = 8; i<51; i++)
    Print( (" "+i).slice(-2),": ",good_input(i),"\t" )

    --- output ---
    8: true 9: true 10: false 11: false 12:
    false
    13: false 14: false 15: false 16: false 17:
    false
    18: false 19: true 20: false 21: false 22:
    false
    23: false 24: false 25: false 26: false 27:
    false
    28: false 29: true 30: false 31: false 32:
    false
    33: false 34: false 35: false 36: false 37:
    true
    38: true 39: true 40: true 41: false 42:
    false
    43: false 44: false 45: false 46: true 47:
    false
    48: true 49: true 50: true
    William James, Dec 1, 2008
    #7
  8. In comp.lang.javascript message <3b83adc5-0b33-4324-b60f-98eec754243d@k3
    6g2000pri.googlegroups.com>, Mon, 1 Dec 2008 08:33:36, "Joe [at]
    subprint.com" <> posted:
    >On Nov 26, 7:11 am, Dr J R Stockton <> wrote:
    >> In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
    >> 6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]
    >> subprint.com" <> posted:
    >>
    >> >> --
    >> >> Lasse

    >>
    >> Please read the newsgroup FAQ more carefully.


    >> It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.
    >>
    >> --
    >>  (c) John



    >With all due respect, Dr. Stockton, I'm a professional


    > in the UI
    >Development industry leading a team of UI Engineers, however, that
    >does not make me an EXPERT with regular expressions.


    They have my sympathy.


    >Also, the FAQ page gave me ZERO insight into my problem, but thanks
    >anyway, Dr.


    The citation was not intended to be of direct help. If you read the FAQ
    carefully, you will discover how newsgroup questions and replies should
    be composed. Following that is a prerequisite for sympathetic treatment
    from all knowledgeable regulars here (except two). Assuming that by UI
    you mean User Interface, that is indeed a UI matter.

    If all else fail, read the instructions. And remember what WSC is said
    to have said.

    --
    (c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
    Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
    No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
    Dr J R Stockton, Dec 1, 2008
    #8
  9. On Dec 1, 12:39 pm, Dr J R Stockton <> wrote:
    > In comp.lang.javascript message <3b83adc5-0b33-4324-b60f-98eec754243d@k3
    > 6g2000pri.googlegroups.com>, Mon, 1 Dec 2008 08:33:36, "Joe [at]
    > subprint.com" <> posted:
    >
    >
    >
    > >On Nov 26, 7:11 am, Dr J R Stockton <> wrote:
    > >> In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
    > >> 6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]
    > >> subprint.com" <> posted:

    >
    > >> >> --
    > >> >> Lasse

    >
    > >> Please read the newsgroup FAQ more carefully.
    > >> It's a good idea to read the newsgroup c.l.j and its FAQ.  See below..

    >
    > >> --
    > >>  (c) John

    > >With all due respect, Dr. Stockton, I'm a professional
    > > in the UI
    > >Development industry leading a team of UI Engineers, however, that
    > >does not make me an EXPERT with regular expressions.

    >
    > They have my sympathy.
    >
    > >Also, the FAQ page gave me ZERO insight into my problem, but thanks
    > >anyway, Dr.

    >
    > The citation was not intended to be of direct help.  If you read the FAQ
    > carefully, you will discover how newsgroup questions and replies should
    > be composed.  Following that is a prerequisite for sympathetic treatment
    > from all knowledgeable regulars here (except two).  Assuming that by UI
    > you mean User Interface, that is indeed a UI matter.
    >
    > If all else fail, read the instructions.  And remember what WSC is said
    > to have said.
    >
    > --
    >  (c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike6.05.
    >  Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
    >  Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> :  about usage of News.
    >  No Encoding. Quotes precede replies. Snip well. Write clearly. Mail noNews.


    I apologize. I'll read over the FAQ for any/all questions going
    forward.

    Thanks!

    Joe
    Joe [at] subprint.com, Dec 2, 2008
    #9
    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,267
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    829
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,917
    vforvikash
    Dec 29, 2008
  4. Joey
    Replies:
    7
    Views:
    457
    Kevin Spencer
    May 24, 2006
  5. Replies:
    5
    Views:
    97
Loading...

Share This Page