阳光地带

风雨雷电

博客园 首页 新随笔 联系 订阅 管理

//找出两段文本中相同的单词短句
<SCRIPT LANGUAGE="JavaScript">
function meizz(a, b, n)
{
    var c=a.length>b.length?b:a;
    if(b==c) b=a; a=c; //少做些循环
    if(!n) n=1;
    var mm = new Array();
    for(var i=0; i<a.length; i++)
    {
        for(var j=i+n; j<=a.length; j++)
        {
            var s = a.substring(i, j);
            if(b.indexOf(s)==-1)
            {
                if(s.length>n)
                {
                    mm[mm.length] = a.substring(i, j-1);
                    i = j-2;
                }
                break;
            }
            else
            {
                if(j==a.length)
                {
                    mm[mm.length]=s;
                }
            }
        }
       
    }
    return mm.Unique().join(","); //去除数组里的重复项
    //return mm.join(",");
}

Array.prototype.Unique = function()
{
  var a = {}; for(var i=0; i<this.length; i++)
  {
    if(typeof a[this[i]] == "undefined")
      a[this[i]] = 1;
  }
  this.length = 0;
  for(var i in a)
    this[this.length] = i;
  return this;
};


var a = "我们是最好的";
var b = "我们不是最差的";
//结果得出"我们"&"是最"
alert("返回的结果:"+ meizz(a, b, 2));
</SCRIPT>

posted on 2007-04-23 10:57  阳光地带  阅读(470)  评论(0编辑  收藏  举报