[Javascript] .match() method

Clancy Jones clancyjones at hotmail.com
Tue Aug 3 00:45:28 CDT 2004


awesome, that did it!  thanks a lot.
Clancy


>From: "Peter Brunone" <peter at brunone.com>
>Reply-To: "[JavaScript List]" <javascript at LaTech.edu>
>To: "'[JavaScript List]'" <javascript at LaTech.edu>
>Subject: RE: [Javascript] .match() method
>Date: Tue, 3 Aug 2004 00:41:19 -0500
>
>Hi again...
>
>	In this case, I'd suggest checking for the existence/"true-ness"
>of each variable, i.e.
>
>	if(aOccur) {
>		if(aOccur.length>=3){
>			recommendation = "Product A"
>			}
>		}
>	}
>
>	This way you skip right over it if aOccur contains nothing.
>
>Cheers,
>
>Peter
>
>-----Original Message-----
>From: javascript-bounces at LaTech.edu On Behalf Of Clancy Jones
>
>Thanks Peter, that has definitely got things rolling.
>
>I now have another problem!
>
>I have a form which has five groups of radio buttons, cat1, cat2 etc.
>Based
>on the user's selection of these radio buttons I want to recommend one
>of
>three products when the form is submitted.  The criteria for this is
>that if
>3 or more of the checked radios have a checked value of a,b,c
>(indicating
>product a, b or c) then that product is the recommended product.  But if
>no
>selection was made to indicate product c for example then i get a
>'length'
>is null or not an object error.  I've tried to trap this with things
>like
>if(aOccur.length) etc but no joy so far.
>
>Here's the code...
>
><script langucat2="JavaScript">
>function processForm(){
>	var cat1Value
>	var cat2Value
>	var cat3Value
>	var cat4Value
>	var cat5Value
>	//get the selected value for each category
>	cat1Value = getRadioValues("cat1");
>	cat2Value = getRadioValues("cat2");
>	cat3Value = getRadioValues("cat3");
>	cat4Value = getRadioValues("cat4");
>	cat5Value = getRadioValues("cat5");
>
>	//concatenate the values into a single string
>	//which might be something like "acbaa"
>	var formSelections =
>cat1Value+cat2Value+cat3Value+cat4Value+cat5Value;
>
>	//work out how many occurrences of
>	//each product indication are in
>	//the formSelections string
>	re = /a/ig;
>	var aOccur = formSelections.match(re)
>	re = /b/ig;
>	var bOccur = formSelections.match(re)
>	re = /c/ig;
>	var cOccur = formSelections.match(re)
>
>	var recommendation
>	//the problem is here -
>	//eg. if no "Product C" selections
>	//were made then cOccur
>	//causes a 'length' is null or not
>	//an object error
>	if(aOccur.length>=3){
>		recommendation = "Product A"
>	}
>	if(bOccur.length>=3){
>		recommendation = "Product B"
>	}
>	if(cOccur.length>=3){
>		recommendation = "Product C"
>	}
>	alert("The recommended product is: " + recommendation);
>}
>
>function getRadioValues(paramRadioGroup){
>	var radioGroup = document.Main[paramRadioGroup]
>	for (var i=0; i<radioGroup.length; i++){
>		if (radioGroup[i].checked)  {
>			return radioGroup[i].value
>		}
>	}
>}
></script>
>
>Any help much appreciated!
>Clancy
>
>
> >From: "Peter Brunone" <peter at brunone.com>
> >
> >Clancy,
> >
> >	I've never tried this before, but the documentation at
> >
> >http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthmatch.as
> >p
> >
> >says this:
> >
> >"If the global flag (g) is not set, Element zero of the array contains
> >the entire match..."
> >
> >	Basically, it looks like when this flag is not set, you only get
>the
> >first occurrence.  FYI, this method is meant to be used with a regular
> >expression (or string containing regex pattern and flags) as the search
>
> >criterion.
> >
> >	Anyway, you can read up at the link above and see if it helps.
> >
> >Cheers,
> >
> >Peter Brunone
> >-----------------------
> >Do the impossible.
> >Go home early.
> >
> >www.EasyListBox.com
> >-----------------------
> >
> >-----Original Message-----
> >From: javascript-bounces at LaTech.edu
> > On Behalf Of Clancy Jones
> >
> >Hi, I haven't used the match() method before but am trying to use it in
>
> >conjunction with the length property to find out how many instances of
> >a
> >
> >substring are contained within a string.
> >
> >I grabbed this example from my Google search:
> >
> ><script language="JavaScript">
> >var s = "Tony was here. Not Bob, Tony's brother, but Tony himself.";
> >var occur = s.match("Tony"); document.write(occur.length); </script>
> >
> >The site listing the above said the output should be 3 but I get 1.  I
> >am using IE6.
> >
> >Can someone point me in the right direction?
> >
> >Thanks a lot,
> >Clancy
>
>
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript

_________________________________________________________________
Check out news, entertainment and more @  http://xtra.co.nz/broadband




More information about the Javascript mailing list