开发者

how to get selected text surrounding context in javascript in different paragraph?

hi I have a few problems:

  1. What might I get the word around the word selected, if the word before and after the selected word given limits only 20 words that surround the selected word?
  2. how to get the correct position if the word in a paragraph have the same word, for example I have a sentence like this: foo laa foo doo then I choose the word "foo" whose position is in between the words laa and doo?
  3. how to get word from a different paragraph?

    for example:

    p1 : I like the red shirt

    p2: my mother did not like the red shirt

    the word I selected is "mother", and I have to take 10 words around the word "mother" that is "I like the red dress" and "I do not like the red shirt."

notes: question No. 2 is able to use the nextSibling and previousSibling?

this is my code i try :

<html>
<head>
<script type="text/javascript">
function getElements(){
  v开发者_如何学JAVAar 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>


This is a perfect example of JavaScript's innerHTML and split() methods. You can loop through the content of all of the p elements. Here's an example of searching in the first paragraph:

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

split(' ') splits the content of the element into an array, separating by the spaces. innerHTML is self explanatory.

Now, to find your words. indexOf() is your friend in this case:

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

Finally, to get surrounding words, just play with the array (this won't work if the occurrence of the string is close to the start or end of the paragraph, namely less than 10 words away:

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

Good luck (no guarantees this code works out of the box)!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜