View Full Version : regexp highlight words

03-17-2004, 05:15 PM

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...

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! :thumbsup:

Willy Duitt
03-17-2004, 05:31 PM
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

<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++){
document.f.t1.value = a.join(' ');
// -->
<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">
<input type="button" value="Acronyms toUpperCase" onClick="capAcrons()">


03-17-2004, 08:08 PM
cheers dude, looks good... will let you know how it goes!!