Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Var question

Status
Not open for further replies.

bryanfl100

Technical User
Dec 2, 2005
17
US
The script below is just a small section of the script but what I wanted was to know is how to make the /Dell/; also equal to other values such as /Dell|Optiplex/; and /Dell|Dimensions/; etc:

<script>
function look_$data[computer.id](s){
var regex = /Dell/;
if (regex.test(s)) {
var look = 1;
} else {
var look = 0;
}
 
I wrote a small test function that shows how you might do something similar:
Code:
function testFunc(pStr, pRexp) {
	var regex = /Dell/; /* the default regexp */
	if (pRexp) regex = new RegExp(pRexp,'gi');
	if (regex.test(pStr)) { 
		var look = 1; 
	} else { 
		var look = 0; 
	}
	return look;
}

alert(testFunc('Test string','Dell|Mac')); /* alerts 0 */
alert(testFunc('Test Dell string','Dell|Mac')); /* alerts 1 */
alert(testFunc('Test Mac string','Dell|Mac')); /* alerts 1 */

alert(testFunc('Test string','Dell|Optiplex')); /* alerts 0 */
The code takes an optional second parameter that contains the regular expression to apply. If the second parameter is not passed, then it defaults to the predefined regexp.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Well the problem is that there are thousands of combinations for /Dell/. Is there a substiution expression for it to understand /Dell|any-other-combination/?
 
Here is an example of what I am trying to do, this code gives me an error:

var regex = /(Dell[a-z][A-Z][0-9])+/;
 
Try this.
[tt]var regex=/^Dell(\|(Optiplex|Dimensions)){0,1}$/i;[/tt]

It would validate Dell, or Dell|Optiplex, or Dell|Dimensions (with an s, are your sure?). If you want to add precision say, it would be this.

[tt]var regex=/^Dell(\|(Optiplex|Dimensions|Precision)){0,1}$/i;[/tt]

etc. But it you think you can sit back and have something general to validate all models, then no.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top