Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javascript string match help

Options
  • 02-08-2007 4:25pm
    #1
    Registered Users Posts: 872 ✭✭✭


    Hi,

    Im trying to search for link text on a page. Basically there are 10 values in an array and i need to check if any links on the page contain the text in the array. If there is a match i need to change the css class of that link.

    Here is what i have so far:

    var english = ["BOU","BRI","BRR","CAM","CAC","HAS","LON","MAN","OXF","TOR"];

    for(i=0;i<english.length;i++){
    if(document.getElementById("row2").innerHTML.match(english))
    {
    alert("match");
    //need to set link css class here
    }
    else
    {
    alert("no match");
    }
    }

    A sample link im comparing is

    <li><a href="page.html?code=BOU">Bournemouth</a></li>

    Im wondering how i can access the node that contains the link so i can set it's css class.

    Thanks for any help, i hope i have made myself clear


Comments

  • Closed Accounts Posts: 161 ✭✭nude_hamster


    hey ok, in an if statement , it needs a boolean result, true or false, the match method returns the mathced string(s) in an array. so a way of
    doing this could be
    var english = ["BOU","BRI","BRR","CAM","CAC","HAS","LON","MAN","OXF","TOR"];
    for(i=0;i<english.length;i++) {
         matchString = english[i];
         //this creates a regular expression to search for and the "i" indicates, ignore the case
         matchExpression = new RegExp(english[i], "i");
         matchResults = document.getElementById("row2").innerHTML.match(matchExpression);
         if(matchResults.length>0)
         {
              alert("match");
              //need to set link css class here
         }
         else
         {
              alert("no match");
         }
    }
    


  • Registered Users Posts: 378 ✭✭sicruise


    Or you could use...
    stringObject.indexOf(searchvalue,fromindex)
    

    i.e.
    var english = ["BOU","BRI","BRR","CAM","CAC","HAS","LON","MAN","OXF","TOR"];
    
    for(i=0;i<english.length;i++){
    if(document.getElementById("row2").innerHTML.indexOf(english[i], 0)>-1)
    {
    alert("match");
    //need to set link css class here
    }
    else
    {
    alert("no match");
    }
    }
    


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Not in anyway trying to rain on your parade but the search is too simple to return accurate results. If the link contains any of your 3 letter codes anywhere in it then it will return true. (e.g. www.brightwater.com will return true) Shouldn't you search for code=XXX ?

    I'm also a bit confused by why you are using javascript for this? Are you pulling the pages into an iframe and searching the iframe for the match? Otherwise don't you already know if the code is on the page? At the very least the script will have to be added (and called) on the page you are searching. Wouldn't if be easier to just do a Ctrl+F ?

    -RD :confused:


Advertisement