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

regex find & match first 4 chars

Status
Not open for further replies.

struth

Programmer
Aug 26, 2001
114
GB
I am looking to find matches to the first 4 letters of a string. I know it's got to use regex but my attempts have just left me banjaxed. But it should be something like....


onClick="matchString('a10109')"



function matchString(string)
{

var mystring = (string);

//now get the first 4 letters of the string
some sort of regex here!!!

// now get the div tags then check their id's for the same first 4 letters
var divs=document.getElementsByTagName("DIV");

for (i=0;i<divs.length;i++)

//use my regex here
if(divs.getElementsByTagName("id")='MYREGEX')
{
divs.style.display='block';
}
}
}


Any help would be appreciated.

&quot;Away from the actual ... everything is virtual&quot;
 
Code:
function matchString(str)
{

//now get the first 4 letters of the string
some sort of regex here!!!

var newString = str.substring(0,4);

etc.............
 
Very helpful thanks but I can't seem to get the matching routine right. Can you help me here ... tia


function loadThread(id)
{
var divs=document.getElementsByTagName("DIV");
var thread = id.substring(0,4);
for (i=0;i<divs.length;i++)
{
if(divs.getElementsById().substring(0,4) = node)
{
divs.style.display='block';
}
}
}


&quot;Away from the actual ... everything is virtual&quot;
 
Your code is fine apart from two things. Firstly, you assign the id.substring to 'thread' but then compare the div substrings to 'node', and secondly you use the single '=' in your comparison line - this is for assignation, and should be a '==' for comparison. Javascript has some leniency for the use of '=' where '==' is needed in this regard, but it's scope is variable and shouldn't be relied on.
 
Thanks for the '==' it solved a problem on another script. However not on this one. Perhaps this is because I am not looking just for divs whose ids match in both characters and length but just the first 4 characters? EG

id = abcdefgh

all these would match

abcd
abcde
abcdef
abcdefg


function loadThread(id)
{
var divs=document.getElementsByTagName("DIV");
var thread = id.substring(0,4);
for (i=0;i<divs.length;i++)
{
if(divs.getElementsById().substring(0,4) == thread)
{
divs.style.display='block';
}
}
}


&quot;Away from the actual ... everything is virtual&quot;
 
If you're not just looking for DIVs then you have a far more basic problem, since you're only comparing *to* DIVs with the getElementsByTagName("DIV") making up the list. What *are* you trying to match against? Any element in the document, or some definable subset?
 
...and not to mention that the code you have is case-sensitive...
 
struth,

You can do something like this (no regex's are needed):

Code:
function matchString(s) {

    //now get the first 4 letters of the string
    var firstFour = s.substr(0, 4);

    // now get the div tags then check their id's for the same first 4 letters
    var divs=document.getElementsByTagName("DIV");

    for (i=0;i<divs.length;i++)
       if(divs[i].id.indexOf(firstFour) > -1) {
           divs[i].style.display='block';
       } else {
           divs[i].style.display='none';
        }
    }
 }

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Careful with that one, cLFlaVA, that'll submatch any string whose id *contains* the four characters, at any index. You'd need to use indexOf(...) == 0 to match only the first four. However, you have spotted what I missed in struth's code - he's not checking against the .id element, but against the root object.
 
Me? I'm just a glitch in the world's programming. I fix things, you see, and the poor thing just isn't designed for that.
 
Thanks guys. As always an education. It works a treat.

&quot;Away from the actual ... everything is virtual&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top