SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search and Replace

    The javascript below is looking for the word 'margaret thatcher' in the body and surrounding it with a link. It's lower-case but the script is ignoring the case so that won't matter. But, the problem is that because Margaret Thatcher is a proper noun, it will be capitalized in the body text yet replaced with lower-case text. How can I modify this script to look for the word while ignoring the case but use the same text it found as the replacement instead of using the text object?


    Code:
    <script type="text/javascript">
    
    searchObj=new Object();
    
    //The following lines define the text that you want to 
    //replace with hyperlinks.  to add a new one just add a new
    //property to the object and then add the text and link 
    //with a comma between them
    searchObj.link1="margaret thatcher,http://www.bbc.co.uk";
    
    function linkWord(obj){
    
      for(i in obj){
        var x = document.getElementById('text').innerHTML;
        var linkStart = '<a href="'+obj[i].substring(obj[i].indexOf(",")+1)+'">';
        var linkEnd = '</a>';
        var reg = new RegExp ('\\b' + obj[i].substring(0, obj[i].indexOf(",")) + '\\b','gim');
        x = x.replace(reg, linkStart + obj[i].substring(0, obj[i].indexOf(",")) + linkEnd);
        document.getElementById('text').innerHTML = x;
        //alert(linkStart);
      }
    }
    </script>
    
    </head> 
    <body onLoad="linkWord(searchObj);"> 
    
    ...

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    One easy solution is to simply write "Margaret Thatcher" with the capital letters in your searchObj.link1. That's what it's getting replaced with anyway.

    The alternative is to capture the searched string and put it in the replacement:

    Code javascript:
    function linkWord(obj){
     
      for(i in obj){
        var x = document.getElementById('text').innerHTML;
        var linkStart = '<a href="'+obj[i].substring(obj[i].indexOf(",")+1)+'">';
        var linkEnd = '</a>';
        var reg = new RegExp ('\\b(' + obj[i].substring(0, obj[i].indexOf(",")) + ')\\b','gim');
        x = x.replace(reg, linkStart + '$1' + linkEnd);
        document.getElementById('text').innerHTML = x;
        //alert(linkStart);
      }
    }

  3. #3
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The index of words I'm working on have all been lower-cased long ago so your REGEXP fix is fantastic! Thank you!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •