Simple Regular Expression Help

  • Thread starter Joe [at] subprint.com
  • Start date
J

Joe [at] subprint.com

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

Lasse Reichstein Nielsen

Joe [at] subprint.com said:
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
 
J

Joe [at] subprint.com

Joe [at] subprint.com said:
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

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
 
L

Lasse Reichstein Nielsen

Joe [at] subprint.com said:
On Nov 24, 4:40 pm, Lasse Reichstein Nielsen <[email protected]>
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
 
D

Dr J R Stockton

In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]

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

Joe [at] subprint.com

In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]

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.   [email protected]     IE7 FF2 Op9 Sf3
 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
 
W

William James

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" <[email protected]> posted:
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.   [email protected]    IE7 FF2 Op9 Sf3
 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
 
D

Dr J R Stockton

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 said:
In comp.lang.javascript message <b4172bf4-299d-4a38-8d2d-32ed3fd34cc5@q2
6g2000prq.googlegroups.com>, Tue, 25 Nov 2008 12:05:27, "Joe [at]

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

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

Joe [at] subprint.com

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" <[email protected]> posted:


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" <[email protected]> posted:
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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top