Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

javascript string match help

  • 02-08-2007 04:25PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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