0

你好我有几个问题:

  1. 如果给定所选单词之前和之后的单词仅限制所选单词周围的 20 个单词,我可能会在所选单词周围得到什么单词?
  2. 如果段落中的单词具有相同的单词,如何获得正确的位置,例如我有这样的句子:foo laa foo doo然后我选择位置在单词laadoo之间的单词“foo”
  3. 如何从不同的段落中获取单词?


    例如:
    p1:我喜欢红衬衫
    p2:我妈妈不喜欢红衬衫


    我选择的词是“妈妈” ,我要在“妈妈”这个词周围取10个字,即“我喜欢红裙子”“我不喜欢红衬衫”。

笔记: 问题2是否可以使用nextSibling和previousSibling?

这是我尝试的代码:

<html>
<head>
<script type="text/javascript">
function getElements(){
  var x = document.getElementsByTagName("body");
  x = x[0].innerHTML;

  x = x.replace(/&(lt|gt);/g, function (strMatch, p1){
            return (p1 == "lt")? "<" : ">";});
  var str  = x.replace(/<\/?[^>]+(>|$)/g, "");



  var emailPattern = /[_a-zA-Z0-9\.]+@[\.a-zA-Z0-9]+\.[a-zA-Z]+/gi;
  var urlPattern = /[a-z]+:\/\/[^\s]+/gi;
  var numberOrSymbolPattern = /[0-9\.,!@#\$%\^\&*\(\)`~_\-=\+|\\{}\[\]\s:;<>\?\/]+/gi;

  //////alert(str);

  var str  = str.replace(emailPattern , " ");
  var str  = str.replace(urlPattern , " ");
  var str  = str.replace(numberOrSymbolPattern , " ");
  //////alert(str);
  var str  = str.replace(/[\n\f\r\t]/g, " ");

   var hilangtandabaca = str.replace(/[.!:;'",?]/g," ");

   var kataptg = hilangtandabaca;
   //alert(kataptg);
   var kata = new Array();
    kata[0] = " is ";
    kata[1] = " the ";
    kata[3] = " of ";
    kata[4] = " a ";
    kata[5] = " or ";
    kata[6] = " for ";
    kata[7] = " from ";
    kata[8] = " in ";
    kata[9] = " this ";
    kata[10] = " and ";
    kata[11] = " on ";
    kata[12] = " with ";
    kata[13] = " my ";

 for(var i=0,regex; i<kata.length; i++){
        var regex = new RegExp(kata[i],"gi");
        kataptg = kataptg.replace(regex," ");
    }

   var select = getSelected()+ "";
   alert(select);
   var index = kataptg.indexOf(select);
   //alert("indeks select text:" + index);
   if (index >= 0) {
    var strBefore = "";
    var strAfter = "";
    //var strOri ="";
    //if (index = -1)
    //strOri = kataptg.substr(index);
    //alert(strOri);
    if (index > 0)
    strBefore = kataptg.substr(0, index);
    //alert(strBefore);
    if (index < kataptg.length - 1)
    strAfter = kataptg.substr(index + select.length, kataptg.length - (index + select.length));
    //alert(strAfter);
    alert("Before: " + strBefore + "\nAfter: " + strAfter);
   }

}


function getSelected() {
   var userSelection;
   if (window.getSelection) {
      userSelection = window.getSelection();
   } else if (document.selection) {
       userSelection = document.selection.createRange();
   }
   return userSelection;
}


</script>
</head>                        
<body>
<h2>About</h2>
<p> my email : a@a.a
<p> my url http://id.wikipedia.org/wiki/URL
<p> my telepon number = 081330782
<p>okey In agriculture, the harvest is the process of gathering mature crops from the fields. Reaping is the cutting of grain or pulse for harvest, typically using a scythe, sickle, or reaper.[1] The harvest marks the end of the growing season, or the growing cycle for a particular crop, and this is the focus of seasonal celebrations of on many religions. On smaller farms with minimal mechanization, harvesting is the most labor-intensive activity of the growing season great yeah. !:;'",?</p>

<p>
<input type="button" onclick="getElements()" value="ambil select text" />
</p>

</body>
</html>
4

1 回答 1

1

这是 JavaScriptinnerHTMLsplit()方法的完美示例。您可以遍历所有p元素的内容。这是第一段中搜索的示例:

contentArray = document.getElementByTagName('p')[0].innerHTML.split(' ')

split(' ')将元素的内容拆分为一个数组,以空格分隔。innerHTML是不言自明的。

现在,找到你的话。indexOf()在这种情况下是你的朋友:

foodex = contentArray.indexOf('foo');
alert('The first occurrence of the string \'foo\' in the text is at word number ' + foodex);

最后,要获取周围的单词,只需使用数组即可(如果字符串的出现靠近段落的开头或结尾,即距离小于 10 个单词,这将不起作用:

alert('I am the 10th word after \'foo\'' + contentArray[foodex + 10 - 1]);

祝你好运(不保证此代码开箱即用)!

于 2010-12-05T04:38:54.260 回答