padolsey / findAndReplaceDOMText

:mag: Find and replace DOM text
776 stars 146 forks source link

findAndReplaceDOMText makes only a single match #15

Closed tomasznowacki closed 10 years ago

tomasznowacki commented 10 years ago

Trying to use version 0.40 to match text elements within a div

var imageLinkSpan = document.createElement('span');
imageLinkSpan.className = 'imageLink';

 findAndReplaceDOMText(document.getElementById("answerField"), {
    find: /\(see\simage\s[^)]*\)/,
    wrap: imageLinkSpan
  }); 

Searching within the following div...

<div id="answerField">
    Tufted astrocytes. Globose tau positive neurofibrillary tangles in neurons. Thorn astrocytes (contain tau along the length of their processes, often binucleated). Gross pathology - midbrain atrophy. 
    <span class="imageLink" onclick="displayImage("images/PSP-pathology_1.png");">
      (see image 1)
    </span>
     (see image 2)
</div>

(the onclick handler is added later, by my own code)

The goal being to wrap each text fragment of type (see image n) with a span of class imageLink. Instead, only the first match is made. The issue doesn't appear to be due to the RegExp, as replacing it with /\w+/ just matches the first word, not all words. I had the same problem with 0.20 and just tried the new version today, with the same results.

Great tool, and thanks for all the work so far!

padolsey commented 10 years ago

Using a global flag on your regular expression should work --

/\(see\simage\s[^)]*\)/g

I think I need to make this clearer in the readme -- I shall do that now :)

tomasznowacki commented 10 years ago

Thanks, works!