Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    Question regexp highlight words

    Hey,

    I have a string of text which might be something like

    Dear {firstname},
    Welcome to {clubName}

    - The text above is loaded in to an iFrame so the user can edit the text.... I have an onload event attached to the iFrame which loads the text in to a variable...

    .. I then need to loop through a list of fields in an array. The script needs to read the variable and search for a pattern of something like

    { + field array name + }

    if it finds one, then it should highlight or make bold or something. I just dont know how to write the regexp part to do it.

    here's what I have which doenst work...

    Code:
    function highlightMarkers()
    	{
    		var documentText = iView.document.body.innerHTML;
    		
    		for (x=0;x<fields.length;x++)
    		{
    			//compare each field in the array to the document text.
    			regexp = "{" + fields[x] + "}";
    			if (documentText.search(regexp) > 0)
    			{
    				//we have found a word which is marker
    				//apply the formatting.
    			}
    		}
    		return false;
    	}
    how do I use the regexp to search for any word which has { word } ??

    thanks for any help!

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    With a little work, the following example should fit your needs.
    I would think all you need to do is load your variable into the array
    and add another function to loop thru the multiple fields
    Code:
    <html> 
    <head> 
    <script type="text/javascript"> 
     <!--// 
      function capAcrons(){
       var acron = new Array('ape','boy','cat','dog','elf','fog'); 
       var a = document.f.t1.value.split(/\s+/g); 
        for(count=0; count<acron.length; count++){ 
         var acronym = acron[count]; 
         var myregex = new RegExp('^'+acronym+'\$', 'i'); 
          for(var i=0; i<a.length; i++){ 
            if(a[i].match(myregex)){ 
               c=a[i].toUpperCase(); 
               a[i]=c; 
            } 
               a[i]=a[i]; 
          } 
        } 
       document.f.t1.value = a.join(' '); 
      } 
    // --> 
    </script> 
    </head> 
    <body bgcolor="white"> 
    <form name="f" method="POST" action="" enctype="text/plain"> 
    <input type="text" name="t1" value=" ape Cat dog cats apes cat fog fogs elves elf " size="50"> 
    <br> 
    <input type="button" value="Acronyms toUpperCase" onClick="capAcrons()"> 
    </form> 
    </body> 
    </html>
    .....Willy

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    cheers dude, looks good... will let you know how it goes!!


  •  

    Posting Permissions

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