2

好的,很久以前,我在 highlight 插件的帮助下制作了一个 jquery 脚本,它使您可以在输入中输入一个术语,该术语在文本中突出显示,然后您可以滚动,单击下一个或上一个按钮,从一个突出显示跨度元素,到另一个。

真的很酷,你可以尝试点击这个链接上的按钮来玩:http: //jsfiddle.net/m6ZMp/

现在我需要highlight/scroll在一些乱七八糟的 html 代码上使用这个脚本,我把它放在与前面示例中的文本相同的演示容器中。搜索字词的突出显示没有问题,但点击我的prevnext按钮时出现错误。这是因为我正在使用它们从一个突出显示的单词滚动到另一个像这样:

count = 0;
$('#next1').click(function (e) {
    var max_length = $('.highlight1').length;
        if (count < max_length) {
            count++;
        } else {
            count = 1;
        }
        e.preventDefault();
        scrollToElement('.highlight1:nth-child(' + count + ')', 400, -150);
    });

    $('#prev1').click(function (e) {
    var max_length = $('.highlight1').length;
        if (count > 1) {
            count--;
        } else {
            count = max_length;
        }
        e.preventDefault();
        scrollToElement('.highlight1:nth-child(' + count + ')', 400, -150);
    })

我有那个计数,用于获取找到的元素的数量,然后在每个 click 函数结束时,我将 intscrollToElement作为 selector 的一部分传递给函数'.highlight1:nth-child(' + count + ')'。现在我假设我遇到了问题,因为在那些凌乱的文档中,高亮显示的术语不再处于同一级别,它们不是同一元素的兄弟或子元素,因此我的功能无法正常工作。

它给了我scrollToElement功能上的错误:

未捕获的类型错误:无法读取 null 的属性“顶部”

您可以在这里查看、测试和编辑情况:http: //jsfiddle.net/9EDSp/

整个脚本如下所示:

$(function () {
    $('#field1').bind('keyup change', function (ev) {
        // pull in the new value
        var searchTerm = $(this).val();

        // remove any old highlighted terms
        $('#demo-container').removeHighlight('span.highlight1');

        // disable highlighting if empty
        if (searchTerm) {
            var terms = searchTerm.split(/\s+/);
            $.each(terms, function (_, term) {
                // highlight the new term
                term = term.trim();
                if (term != "") $('#demo-container').highlight(term, 'highlight1');
            });

        }
    }).triggerHandler('change');

});

    /**  scroll to element function **/

    function scrollToElement(selector, time, verticalOffset) {
        time = typeof (time) != 'undefined' ? time : 500;
        verticalOffset = typeof (verticalOffset) != 'undefined' ? verticalOffset : 0;
        element = $(selector);
        offset = element.offset();
        offsetTop = offset.top + verticalOffset + $('#demo-container').scrollTop();
        $('#demo-container').animate({
            scrollTop: offsetTop
        }, time);
    }

    /**document ready**/
    $(document).ready(function () {
        count = 0;
        /* scroll to 150px before .highlight with animation time of 400ms */
        $('#next1').click(function (e) {
        var max_length = $('.highlight1').length;
            if (count < max_length) {
                count++;
            } else {
                count = 1;
            }
            e.preventDefault();
            scrollToElement('.highlight1:nth-child(' + count + ')', 400, -150);
        });

        $('#prev1').click(function (e) {
        var max_length = $('.highlight1').length;
            if (count > 1) {
                count--;
            } else {
                count = max_length;
            }
            e.preventDefault();
            scrollToElement('.highlight1:nth-child(' + count + ')', 400, -150);
        })
    });

我是否只需要使用不同的选择器从一个术语滚动到另一个术语,或者只是我需要比当前代码更多的东西?

欢迎任何意见、建议或帮助......

4

2 回答 2

1

尝试使用 $.each() 循环选择所有元素 highlight1,并将每个元素添加到数组中。使用当前计数作为索引,并为您需要移动到的元素选择正确的索引。然后滚动到该元素。所以传递一个 jQuery 对象/元素而不是选择器,并将 ScrollToElement 函数中的元素映射到您传递的对象,而不是您传递的选择器。使用 nth-child 选择该父级的 (n) CHILD 元素,而不是 $('.highlight1') 返回的列表中的 (N) 项。

或者,您可以使用 .index() 在您所在的索引/计数处选择 highlight1 项。但是请记住,索引将比您的计数少一个。基于数组!

希望这对我解释它的方式有意义。

于 2013-10-03T16:37:58.880 回答
1
    I have modified the above code to work similar to page search in chrome . 
    Also added  below features
    1)added a stop word filter to remove unwanted terms being highlighted (eg a-z , an ,at etc)
    2)Change in highlight color between term navigation.




    jsp file:
 -----------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
    language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>highlight</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR"
    content="Rational® Application Developer for WebSphere® Software">


<script src="<%=request.getContextPath()%>/js/jquery.js"
    type="text/javascript"></script>    
    <script src="<%=request.getContextPath()%>/js/highlight2.js"
    type="text/javascript"></script>    

<style type="text/css">
    @import url('http://getbootstrap.com/2.3.2/assets/css/bootstrap.css');

#outer{
    width:500px;
    height:300px;
    padding-top:50px;
}
#demo-container{
   width:200%;
   height:150%;
    overflow:auto;
}

.dock{
    position:fixed;
    background:#FFFFFF;
}

.prev-highlight {
    -moz-border-radius: 5px;
    /* FF1+ */
    -webkit-border-radius: 5px;
    /* Saf3-4 */
    border-radius: 5px;
    /* Opera 10.5, IE 9, Saf5, Chrome */
    -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7);
    /* FF3.5+ */
    -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7);
    /* Saf3.0+, Chrome */
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7);
    /* Opera 10.5+, IE 9.0 */
}

.prev-highlight{
    background-color: #fff34d;
     padding:1px 4px;
      margin:0 -4px;
}



 .prev-highlight-selected {
background-color: #FF8000;
}
  </style>



<script type="text/javascript">
$(window).load(function(){
$(function () {
    $('#field1').bind('keyup change', function (ev) {
        // pull in the new value
        var searchTerm = $(this).val();
        searchTerm=searchTerm.removeStopWords();

        // remove any old highlighted terms
        $('#demo-container').removeHighlight('span.prev-highlight');

        // disable highlighting if empty
        if (searchTerm) {
            var terms = searchTerm.split(/\s+/);
            $.each(terms, function (_, term) {
                // highlight the new term
               // term = term.trim();
                if (term != "") $('#demo-container').highlight(term, 'prev-highlight');
            });

        }
    }).triggerHandler('change');

});

    /**  scroll to element function **/

    function scrollToElement(selector, time, verticalOffset) {

         selectedKeyword = $('.prev-highlight-selected');
     if (selectedKeyword) selectedKeyword.removeClass('prev-highlight-selected');
      $(selector).addClass('prev-highlight-selected');


        time = typeof (time) != 'undefined' ? time : 500;
        verticalOffset = typeof (verticalOffset) != 'undefined' ? verticalOffset : 0;

        element = $(selector);
        offset = element.offset();
      //  alert(offset.top);
        offsetTop = offset.top + verticalOffset + $('#demo-container').scrollTop();
        $('#demo-container').animate({
            scrollTop: offsetTop
        }, time);

    }

    /**document ready**/
    $(document).ready(function () {

        count = 0;


        /* scroll to 150px before .highlight with animation time of 400ms */
        $('#next1').click(function (e) {        
        count++;
        var max_length = $('.prev-highlight').length;
        var child =  (count-1)% max_length ;

            if (count == max_length) {
                count=0;
            } 
                        e.preventDefault();
            scrollToElement('.prev-highlight:eq(' + child + ')', 400, -150);
        });

        $('#prev1').click(function (e) {    
        var max_length = $('.prev-highlight').length;       
        count--;
        if (count <=0)
        {                
        count=max_length;                        
        }

   var  child =  (count-1)% max_length ;

            e.preventDefault();
            scrollToElement('.prev-highlight:eq(' + child + ')', 400, -150);
        });
    });
});

</script>


</head>
<body>
  <div class="dock">
<input class="span3" id="field1" name="field1" value="external and a internal you z" type="text">
<input type="submit" value="Search">
        <a class="btn btn-primary btn-mini" id="prev1" href="#"><i class="icon-arrow-up icon-white"></i></a>
        <a class="btn btn-primary btn-mini" id="next1" href="#"><i class="icon-arrow-down icon-white"></i></a>
    </div>






  <div id="outer">
<div id="demo-container">
    <p class=default>
      <span style="color: #ffc000; font-size: 18pt; font-family: Times New Roman">
        <i><b>Vanity URL creation</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 1: </b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        We have to receive the domain name.
      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        For example,
      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 16pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 16pt; font-family: Times New Roman">
        <b>www. training.com or ideas. .com. </b>
      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 16pt; font-family: Times New Roman">
        <b>It could be an individual domain name or it could be sub domain of  .com.</b>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 16pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 2:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        We need to check with business whether this should be only  Honeywell use or this
        is for both  external.
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 3:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        We need to check with the customer if https should work for this vanity domain or not. If
        https is expected to work, we have to convey them that this involves additional
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b>SSL certificate cost.</b>
      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 4:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>

    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        For example, when user access ideas. .com, they should be redirected to .
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        Here is the sample,
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        Please note that the IP address, Port number and the log file pathfile name will change
        for each of the web server.
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 6:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        If the application is expected to work in https, then we have to put two entries. One with
        http port and other with https port.
      </span>
    </p>

      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>


     <p class=default>

    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        We need to check with business whether this should be only  Honeywell use or this
        is for both  external.
      </span>
    </p>

    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 3:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>

    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>

      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        If the application to which this vanity domain is pointing to is deployed in BK, then we
        will define the vhost in Bendix/King apache.
      </span>
    </p>
    <p class=ListaParagraph>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>


    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 7:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        Once the above step is done, we can validate the vhost entry using local host file. We
        have to validate before we make DNS entry. To test with local host file, you have to find
        the internal facing IP of apache (internal CSS IP) and external facing IP. This is usually
        documented at the top of the apache include file. Refer the snapshot below.
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        Find below the sample local host file entry. The local host file is usually found in Win
        XP under the path
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b>C:\WINDOWS\system32\drivers\etc\.</b>
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
         The name of the file is "
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b>hosts</b>
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        ". If the domain is suppose to work from both internal and external network, then we have
        to validate with both the IPs
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b>  qideas. .com</b>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 8:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        Once validated, we have to raise DNS ticket. If the URL is meant to be for internal, then
        we just need to raise a ticket with  DNS group. If it is for both, please create
        tickets for both the group.
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <i>Example CRQ Number is</i>
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b> </b>
      </span>
      <span style="color: #548dd4; font-size: 12pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #ff0000; font-size: 12pt; font-family: Times New Roman">
        <i><b>Step 9:</b></i>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        Once the DNS ticket is executed, validate with browser and then also with
      </span>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">

      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>
    <p class=default>
      <span style="color: #000000; font-size: 12pt; font-family: Times New Roman">
        <b></b>
      </span>
    </p>
    </div>
    </div>
  </body>
</html>








-----------------
highlight2 javascript file:


jQuery.fn.highlight = function (pat, className) {

    function innerHighlight(node, pat) {
        var skip = 0;
        if (node.nodeType == 3) {
            var pos = node.data.toUpperCase().indexOf(pat);
            if (pos >= 0) {
                var spannode = document.createElement('span');
                spannode.className = className || 'prev-highlight';
                var middlebit = node.splitText(pos);
                var endbit = middlebit.splitText(pat.length);
                var middleclone = middlebit.cloneNode(true);
                spannode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(spannode, middlebit);
                skip = 1;
            }
        } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
            for (var i = 0; i < node.childNodes.length; ++i) {
                i += innerHighlight(node.childNodes[i], pat);
            }
        }
        return skip;
    }
    return this.each(function () {
        innerHighlight(this, pat.toUpperCase());
    });
};

jQuery.fn.removeHighlight = function (classNames) {
    function newNormalize(node) {
        for (var i = 0, children = node.childNodes, nodeCount = children.length; i < nodeCount; i++) {
            var child = children[i];
            if (child.nodeType == 1) {
                newNormalize(child);
                continue;
            }
            if (child.nodeType != 3) {
                continue;
            }
            var next = child.nextSibling;
            if (next == null || next.nodeType != 3) {
                continue;
            }
            var combined_text = child.nodeValue + next.nodeValue;
            new_node = node.ownerDocument.createTextNode(combined_text);
            node.insertBefore(new_node, child);
            node.removeChild(child);
            node.removeChild(next);
            i--;
            nodeCount--;
        }
    }
    var selectors = classNames;
    if(Object.prototype.toString.call(classNames) === '[object Array]')
      selectors = classNames.join(',');

    return this.find(selectors).each(function () {
        var thisParent = this.parentNode;
        thisParent.replaceChild(this.firstChild, this);
        newNormalize(thisParent);
    }).end();
};



String.prototype.removeStopWords = function() {
    var x;
    var y;
    var word;
    var stop_word;
    var regex_str;
    var regex;
    var cleansed_string = this.valueOf();
    var stop_words = new Array(
        'a',
        'about',
        'above',
        'across',
        'after',
        'again',
        'against',
        'all',
        'almost',
        'alone',
        'along',
        'already',
        'also',
        'although',
        'always',
        'among',
        'an',
        'and',
        'another',
        'any',
        'anybody',
        'anyone',
        'anything',
        'anywhere',
        'are',
        'area',
        'areas',
        'around',
        'as',
        'ask',
        'asked',
        'asking',
        'asks',
        'at',
        'away',
        'b',
        'back',
        'backed',
        'backing',
        'backs',
        'be',
        'became',
        'because',
        'become',
        'becomes',
        'been',
        'before',
        'began',
        'behind',
        'being',
        'beings',
        'best',
        'better',
        'between',
        'big',
        'both',
        'but',
        'by',
        'c',
        'came',
        'can',
        'cannot',
        'case',
        'cases',
        'certain',
        'certainly',
        'clear',
        'clearly',
        'come',
        'could',
        'd',
        'did',
        'differ',
        'different',
        'differently',
        'do',
        'does',
        'done',
        'down',
        'down',
        'downed',
        'downing',
        'downs',
        'during',
        'e',
        'each',
        'early',
        'either',
        'end',
        'ended',
        'ending',
        'ends',
        'enough',
        'even',
        'evenly',
        'ever',
        'every',
        'everybody',
        'everyone',
        'everything',
        'everywhere',
        'f',
        'face',
        'faces',
        'fact',
        'facts',
        'far',
        'felt',
        'few',
        'find',
        'finds',
        'first',
        'for',
        'four',
        'from',
        'full',
        'fully',
        'further',
        'furthered',
        'furthering',
        'furthers',
        'g',
        'gave',
        'general',
        'generally',
        'get',
        'gets',
        'give',
        'given',
        'gives',
        'go',
        'going',
        'good',
        'goods',
        'got',
        'great',
        'greater',
        'greatest',
        'group',
        'grouped',
        'grouping',
        'groups',
        'h',
        'had',
        'has',
        'have',
        'having',
        'he',
        'her',
        'here',
        'herself',
        'high',
        'high',
        'high',
        'higher',
        'highest',
        'him',
        'himself',
        'his',
        'how',
        'however',
        'i',
        'if',
        'important',
        'in',
        'interest',
        'interested',
        'interesting',
        'interests',
        'into',
        'is',
        'it',
        'its',
        'itself',
        'j',
        'just',
        'k',
        'keep',
        'keeps',
        'kind',
        'knew',
        'know',
        'known',
        'knows',
        'l',
        'large',
        'largely',
        'last',
        'later',
        'latest',
        'least',
        'less',
        'let',
        'lets',
        'like',
        'likely',
        'long',
        'longer',
        'longest',
        'm',
        'made',
        'make',
        'making',
        'man',
        'many',
        'may',
        'me',
        'member',
        'members',
        'men',
        'might',
        'more',
        'most',
        'mostly',
        'mr',
        'mrs',
        'much',
        'must',
        'my',
        'myself',
        'n',
        'necessary',
        'need',
        'needed',
        'needing',
        'needs',
        'never',
        'new',
        'new',
        'newer',
        'newest',
        'next',
        'no',
        'nobody',
        'non',
        'noone',
        'not',
        'nothing',
        'now',
        'nowhere',
        'number',
        'numbers',
        'o',
        'of',
        'off',
        'often',
        'old',
        'older',
        'oldest',
        'on',
        'once',
        'one',
        'only',
        'open',
        'opened',
        'opening',
        'opens',
        'or',
        'order',
        'ordered',
        'ordering',
        'orders',
        'other',
        'others',
        'our',
        'out',
        'over',
        'p',
        'part',
        'parted',
        'parting',
        'parts',
        'per',
        'perhaps',
        'place',
        'places',
        'point',
        'pointed',
        'pointing',
        'points',
        'possible',
        'present',
        'presented',
        'presenting',
        'presents',
        'problem',
        'problems',
        'put',
        'puts',
        'q',
        'quite',
        'r',
        'rather',
        'really',
        'right',
        'right',
        'room',
        'rooms',
        's',
        'said',
        'same',
        'saw',
        'say',
        'says',
        'second',
        'seconds',
        'see',
        'seem',
        'seemed',
        'seeming',
        'seems',
        'sees',
        'several',
        'shall',
        'she',
        'should',
        'show',
        'showed',
        'showing',
        'shows',
        'side',
        'sides',
        'since',
        'small',
        'smaller',
        'smallest',
        'so',
        'some',
        'somebody',
        'someone',
        'something',
        'somewhere',
        'state',
        'states',
        'still',
        'still',
        'such',
        'sure',
        't',
        'take',
        'taken',
        'than',
        'that',
        'the',
        'their',
        'them',
        'then',
        'there',
        'therefore',
        'these',
        'they',
        'thing',
        'things',
        'think',
        'thinks',
        'this',
        'those',
        'though',
        'thought',
        'thoughts',
        'three',
        'through',
        'thus',
        'to',
        'today',
        'together',
        'too',
        'took',
        'toward',
        'turn',
        'turned',
        'turning',
        'turns',
        'two',
        'u',
        'under',
        'until',
        'up',
        'upon',
        'us',
        'use',
        'used',
        'uses',
        'v',
        'very',
        'w',
        'want',
        'wanted',
        'wanting',
        'wants',
        'was',
        'way',
        'ways',
        'we',
        'well',
        'wells',
        'went',
        'were',
        'what',
        'when',
        'where',
        'whether',
        'which',
        'while',
        'who',
        'whole',
        'whose',
        'why',
        'will',
        'with',
        'within',
        'without',
        'work',
        'worked',
        'working',
        'works',
        'would',
        'x',
        'y',
        'year',
        'years',
        'yet',
        'you',
        'young',
        'younger',
        'youngest',
        'your',
        'yours',
        'z'
    );


    words = cleansed_string.match(/[^\s]+|\s+[^\s+]$/g);

    // Review all the words
    for(x=0; x < words.length; x++) {
        // For each word, check all the stop words
        for(y=0; y < stop_words.length; y++) {
            // Get the current word
            word = words[x].replace(/\s+|[^a-z]+/ig, "");   // Trim the word and remove non-alpha

            // Get the stop word
            stop_word = stop_words[y];

            // If the word matches the stop word, remove it from the keywords
            if(word.toLowerCase() == stop_word) {
                // Build the regex
                regex_str = "^\\s*"+stop_word+"\\s*$";      // Only word
                regex_str += "|^\\s*"+stop_word+"\\s+";     // First word
                regex_str += "|\\s+"+stop_word+"\\s*$";     // Last word
                regex_str += "|\\s+"+stop_word+"\\s+";      // Word somewhere in the middle
                regex = new RegExp(regex_str, "ig");

                // Remove the word from the keywords
                cleansed_string = cleansed_string.replace(regex, " ");
            }
        }
    }
    return cleansed_string.replace(/^\s+|\s+$/g, "");
}
于 2013-11-25T18:42:34.850 回答