title: How can I get the matched element's parent div attribute by regular express using Javascript
body: <p>Have a html string see below.</p>
  
  <pre><code>&lt;div sentence="11"&gt;
    &lt;p&gt;
      how are you Tom, how are you Tom
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;div sentence="12"&gt;
    &lt;p&gt;
      how are you Tom
    &lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>When user select the name 'Tom', I will replace the string 'Tom' using a name tag with some html style. The result what I want see below.</p>
  
  <pre><code>&lt;div sentence="11"&gt;
    &lt;p&gt;
      how are you &lt;span class="ano-subject" data-oristr="Tom" data-offset="1" data-sentence="11"&gt;&lt;NAME&gt;&lt;/span&gt;, how are you &lt;span class="ano-subject" data-oristr="Tom" data-offset="2" data-sentence="11"&gt;&lt;NAME&gt;&lt;/span&gt;
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;div sentence="12"&gt;
    &lt;p&gt;
      how are you &lt;span class="ano-subject" data-oristr="Tom" data-offset="1" data-sentence="12"&gt;&lt;NAME&gt;&lt;/span&gt;
    &lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I will using the code below to do the replace.</p>
  
  <pre><code>var content = HTML CODE ABOVE;
  var selectText = 'Tom';
  var regex = new RegExp('(?=\\s|^|\\b)(?:' + selectText + ')(?=\\s|$|\\b)', "g");
  var pre_sentence = 0;
  var offset = 0；
  content = content.replace(regex, function(match, position) {
  
      var curr_sentence = ???; // how can I get this element's parent div attribute 'sentence' ?
  
      if（pre_sentence != cur_sentence）{
          // this is new sentence.
          offset = 1;
          pre_sentence = curr_sentence;
      } else {
          offset++;
      }
      var replace = '&lt;span class="ano-subject" data-oristr="Tom" data-offset="'+offset+'" data-sentence="'+curr_sentence+'"&gt;&lt;NAME&gt;&lt;/span&gt;';
  
      return replace;
  });
  </code></pre>