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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching 1

Status
Not open for further replies.

KevinFSI

Programmer
Joined
Nov 17, 2000
Messages
582
Location
US
I'm sure I could find my answer in a keyword search, but the search is down at the moment, so I apologize if I'm asking something that has been asked a million times.

All I want to be able to do is see if my variable matches another variable. I'm having no trouble doing this in a "case sensitive" situation, but as soon as I try to do a "case insensitive" match, I get screwed up. Here's a real stripped down version of what I'm doing.
Code:
var myVar1 = form.text1.value;
var myVar2 = form.text2.value;

myVar1.test(myVar2);
Now, as long as my two form fields are exactly the same, I get a "true." What I want though is to be able to:
1. make the search case insensitive
2. get a true if ANY part of text1 matches text2. For example, if text1=Kevin and text2=kev I want to get a "true."

So, I've tried search(), match(), and text() and just can't get the syntax right. Thanks in advance for any help you all can give me.

Kevin
slanek@ssd.fsi.com
 
//Just convert the input to upper-case (or lower-case).

<input type=&quot;text&quot; name=&quot;text1&quot; value=&quot;&quot; onChange=&quot;javascript:this.value=this.value.toUpperCase();&quot;>
<input type=&quot;text&quot; name=&quot;text2&quot; value=&quot;&quot; onChange=&quot;javascript:this.value=this.value.toUpperCase();&quot;>
//This makes it upper-caser in the text box.

//I think
var mvVar1 = form.text1.value.toUpperCase;
//also works

//Then do your
myvar1.test(myvar2);

Hope this helps.
 
Thanks, that will do the trick and I really appreciate it, but I was hoping someone could tell me why it wasn't working using the regExp().

I've tried all of the following to try to make it case insensitive:
Code:
myVar1 = form.text1.value;
myVar2 = form.text2.value;

myVar1.match(/myVar2/i);
OR
myVar1.match(/\bmyVar2\b/gi);
OR
EVERYTHING ELSE!!!
What the heck am I doing wrong?

Gerry, I really do appreciate it though. I hadn't thought of doing that and it DOES work, so if nothing else, at least I can press on with my project. Kevin
slanek@ssd.fsi.com
 
The problem is, the way you are doing it myVar1.match(/myVar2/i) is actually soing a search for &quot;myVar2&quot; the string, not the value of &quot;myVar2&quot;.

What you need to do to pass a variable to a regular expression is use:
new RegExp(&quot;pattern&quot;[, &quot;flags&quot;])

Take a look at the script below, you can test it out just the way it is alone and see what the output is.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

// This function is for convenience to output the info
function writeInfo(re,myVar1,vFind){
return (&quot;Search for <FONT COLOR=#CC0000>&quot; + re + &quot;</FONT> in <FONT COLOR=#CC0000>&quot; + myVar1 + &quot;</FONT> returns: <FONT COLOR=#CC0000><B>&quot; + vFind + &quot;</B></FONT><BR><BR>&quot;);
}

myVar1 = &quot;Kevin&quot;;
myVar2 = &quot;kev&quot;;

// OUTPUTS: Search for /myVar2/i in Kevin returns position: -1
re = /myVar2/i; // Create Regular expression
vFind = myVar1.search(re); // Search String
document.write(writeInfo(re,myVar1,vFind));

// Add &quot;myVar2&quot; to the string
myVar1 = &quot;Kevin myVar2&quot;;

// OUTPUTS: Search for /myVar2/i in Kevin myVar2 returns position: 6
re = /myVar2/i; // Create Regular expression
vFind = myVar1.search(re);
document.write(writeInfo(re,myVar1,vFind));

// OUTPUTS: Search for /kev/i in Kevin myVar2 returns position: 0
re = new RegExp(myVar2, &quot;i&quot;); // Create Regular expression
vFind = myVar1.search(re); // Create Regular expression
document.write(writeInfo(re,myVar1,vFind));

//-->
</SCRIPT> - tleish
 
A thousand thank yous!!!

I really appreciate it! Kevin
slanek@ssd.fsi.com
 
tleish,
The script works great when I have myVar1 & myVar2 set to hard-coded values (i.e. &quot;Kev&quot; and &quot;Kevin&quot;) but as soon as I try to set them equal to form values (i.e. myVar1 = myForm.myElement.value) I get all kinds of problems. I'm not really sure what to try at this point.

Any hints? Kevin
slanek@ssd.fsi.com
 
tleish,
I got it worked out. I did new RegExp(&quot;^&quot;+myVar1, &quot;i&quot;) and it worked.

Thanks again for the help! Kevin
slanek@ssd.fsi.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top