替换字符串-如何一次替换每个单词 [英] Replace string - How to replace each word once

查看:98
本文介绍了替换字符串-如何一次替换每个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml字典,如下所示.

I have an xml dictionary as shown below.

<word definition="The primary income-earner in a household">bread winner</word>
<word definition="One who wins, or gains by success in competition, contest, or gaming">winner</word>



每当我的html中有一个字典中的单词时,该单词将被链接和定义作为标题替换.当链接悬停时,用户应该看到定义.



Whenerver there is a word from dictionary in my html, that word will be replaced with link and definition as title. When link is hovered, user should see the definition.

var allwords = xmlDoc.getElementsByTagName("word");
for (var i=0; i<allwords.length; i++)
    {
            var name = allwords[i].lastChild.nodeValue;
            
            var linked = ''<a href ="#" title="'' +  allwords[i].lastChild.nodeValue + '': '' + allwords[i].getAttribute("definition") + ''">'' + allwords[i].lastChild.nodeValue + ''</a>''; 
    }


这是我的替补


Here is my replacer

function replacer(oldstring, newstring) {
document.body.innerHTML = document.body.innerHTML.replace(oldstring, newstring);
}



但是问题是
一旦面包优胜者更改为链接形式,优胜者也将更改,因为面包优胜者包括优胜者,优胜者更改了两次,并且所有代码混在一起.

我问是否有一种方法,一旦面包优胜者更改了优胜者,就不应再更改了.

在此先感谢!



But problem is
once bread winner changes to linked form, also winner changes since bread winner includes winner, winner changes twice, and all the code mixes up.

I am asking if there is a way, once bread winner changes winner should not change anymore.

Thanks in advance!

推荐答案

这是我的第一个念头:
第一步,将您在定义列表中找到的HTML正文中的所有单词替换为修改后的表格.像winner会变成例如xxx__winner__xxx.稍后执行替换部分时,请使用您的构造替换修改后的表单.当然,只有以不形成将要插入HTML的单词的方式进行修改时,这才能可靠地起作用.因此,前缀"XXX__"和后缀"__XXX"可能对您有用,但也可能不起作用.既然您最清楚自己的内容是什么,我相信您会想出一个适当的前缀和后缀.

干杯!

-MRB
This was my first thought:
In a first step replace all words in the HTML body found in your definition list by a modified form. Like winner would become for instance xxx__winner__xxx. When doing the replacing part later on replace the modified form with you construct. This will only work reliably of course if the modification is made in a way that will not form a word that will be inserted into your HTML. So a prefix of "XXX__" and a postfix of "__XXX" might work for you but it might also not work. Since you know best what your content will be I''m sure you''ll come up with an appropriate prefix and suffix.

Cheers!

-MRB


我认为最好的方法是递归函数 [ ^ ].
有一个要求:带有单词的数组必须按长度排序.

此递归函数的作用很简单:

1)取一个字符串并找到第一个出现的字符串(不区分大小写).
2)将该行左移并搜索下一个单词(我们知道当前单词不存在),直到该行太短而无法包含单词为止.
3)将原始文字替换为链接.
4)选取右侧并搜索当前单词.
如果找不到当前单词,则返回字符串(部分).

作为递归,将字符串分成小部分,用链接替换单词,并返回连接的字符串.

I think the best approach is a recursive function [^].
There is one requirement: the array with words must be sorted on length.

What this recursive function does is simple:

1) take a string and find the first occurence (case insensitive).
2) split the line in a leftpart and search for the next word (we know that the current word is not there) until the line is too short to contain words.
3) replace the original text with the link.
4) take the rightpart and search for the current word.
If the current word is not found then the string (part) is returned.

Being recursive, the string is split into small parts, words are replaced with links and a joined string is returned.

<script type="text/javascript">

// here your own array from xml, sorted on length
var allwords = ["bread winner", "winner", "win", "w"];

function processString(line, index)
{
  // search for the first occurence (case insensitive) in the line
  var i = line.search(new RegExp(allwords[index], "i"));

  if (i == -1)
  {
    // Not found
    if (index < allwords.length-1)
    {
      // We have more words to find
      return processString(line, index+1)
    }
    else
    {
      // This part ends here
      return line;
    }
  }
  else
  {
    // Word was found, split line into three parts
    var leftpart = line.substr(0, i);
    var word = line.substr(i, allwords[index].length);
    var rightpart = line.substr(i + allwords[index].length);

    var result = "";

    //continue search with another word (if available)
    //and the part is long enough
    if (index < allwords.length-1)
      if (leftpart.length>allwords[index+1].length)
        result = processString(leftpart, index+1);
      else
        result = leftpart;
    else
      result = leftpart;

    //insert here the description from xml
    //the original word is preserved
    result += word.link("www.yourdomain.com?word=" + word);

    //continue search with the current word if the part is long enough
    if (rightpart.length>allwords[index].length)
      result += processString(rightpart, index);
    else
      result += rightpart;

    //left+word+right
    return result;
  }
}

// test string
var str = "win Every bread Winner in the world! Winner win Every bread winner on earth winner!";

//start processing, just one call
str = processString(str, 0);

//this will show the result
document.write(str + "<br />");

//this will show the html
alert(str);

</script>


这篇关于替换字符串-如何一次替换每个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆