开发者

String replace function - how to skip if replaced?

开发者 https://www.devze.com 2023-03-13 20:05 出处:网络
I have an xml file with words and definitions. <word definition=\"this is my definition\">myWord</word>

I have an xml file with words and definitions.

<word definition="this is my definition">myWord</word>

When my html page has myWord, I want to change myWord into a link with its title holds the definition of my word.

Basicly its something like this;

myWord turns into

<a href="#" title="this is my definition">myWord </a> 

To link from xml I am using this code. But this code links a several times, even words in definition get linked. So I want to check if it is al开发者_StackOverflowread linked, if so, skip.

x=xmlDoc.getElementsByTagName('word');
for (i=0;i' + x.item(i).attributes[0].textContent + ': ' + x.item(i).attributes[1].textContent + '\', this);" onMouseOut="doClear();">' +x[i].childNodes[0].nodeValue + '';
    replacer(text, linked);     

I have a replacer function

function replacer(oldstring, newstring) {
 if(oldstring inside replaced tag)
  {
       //skip replacement
  }

 else
  {
    document.body.innerHTML = document.body.innerHTML.replace(/oldstring/, newstring);
  }
}


I want to skip replace if my oldstring is inside

<span class="replaced">Replaced text</span>

How can I write if section of this code.

I have been struggling over this for days. Please help me! Thank in advance!


XSLT will be better way to perform this:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="*|@*|text()">
       <xsl:copy>
          <xsl:apply-templates select="*|@*|text()"/>
       </xsl:copy>
    </xsl:template>
    <xsl:template match="word">
        <a href="#" title="<xsl:copy-of select="data(@description)"/>">
          <xsl:copy-of select="data(.)"/></a>
    </xsl:template>
</xsl:stylesheet>

Contact me if any details required

0

精彩评论

暂无评论...
验证码 换一张
取 消