當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JS高效关键字搜索---转
生活随笔
收集整理的這篇文章主要介紹了
JS高效关键字搜索---转
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文地址: http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html
?
重點(diǎn)關(guān)注評(píng)論中的代碼..
?
| var treeSearch = { ????makeTree: function(strKeys) { ????????"use strict"; ????????var tblCur = {}, ????????????tblRoot, ????????????key, ????????????str_key, ????????????Length, ????????????j, ????????????i ????????????; ????????tblRoot = tblCur; ????????for ( j = strKeys.length - 1; j >= 0; j -= 1) { ????????????str_key = strKeys[j]; ????????????Length = str_key.length; ????????????for ( i = 0; i < Length; i += 1) { ????????????????key = str_key.charAt(i); ????????????????if (tblCur.hasOwnProperty(key)) { //生成子節(jié)點(diǎn)? ????????????????????tblCur = tblCur[key]; ????????????????} else { ????????????????????tblCur = tblCur[key] = {}; ????????????????} ????????????} ????????????tblCur.end = true; //最后一個(gè)關(guān)鍵字沒有分割符 ????????????tblCur = tblRoot; ????????} ????????return tblRoot; ????}, ????search: function(content, tblRoot) { ????????"use strict"; ????????var tblCur, ????????????p_star = 0, ????????????n = content.length, ????????????p_end, ????????????match,? //是否找到匹配 ????????????match_key, ????????????match_str, ????????????arrMatch = [],? //存儲(chǔ)結(jié)果 ????????????arrLength = 0?? //arrMatch的長度索引 ????????????; ?? ????????while (p_star < n) { ????????????tblCur = tblRoot; //回溯至根部 ????????????p_end = p_star; ????????????match_str = ""; ????????????match = false; ????????????do { ????????????????match_key = content.charAt(p_end); ????????????????if (!(tblCur = tblCur[match_key])) { //本次匹配結(jié)束 ????????????????????p_star += 1; ????????????????????break; ????????????????}else{ ????????????????????match_str += match_key; ????????????????} ????????????????p_end += 1; ????????????????if (tblCur.end === true) //是否匹配到尾部? //找到匹配關(guān)鍵字 ????????????????{ ????????????????????match = true; ????????????????} ????????????} while (true); ?? ????????????if (match === true) { //最大匹配 ????????????????arrMatch[arrLength] = { //增強(qiáng)可讀性 ????????????????????key: match_str, ????????????????????begin: p_star - 1, ????????????????????end: p_end ????????????????}; ????????????????arrLength += 1; ????????????????p_star = p_end; ????????????} ????????} ????????return arrMatch; ????} }; |
使用實(shí)例:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function test(strContent, strKeys) { ????var arrMatch, ????????tblRoot = treeSearch.makeTree(strKeys), ????????t = new Date(); ?? ?? ????arrMatch = treeSearch.search(strContent, tblRoot); ?? ????console.log("time is: " + (new Date() - t) + "mm"); ?? ????console.log(arrMatch); } var s = (function() { ????var Things = [' ', '\n', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; ????var s = ""; ????for (var i = 1000000; i >= 0; i--) { ????????s += Things[parseInt(Math.random() * Things.length) % Things.length] ????}; ????return s; })() test(s, ["abc", "efge", "fun", "tree"]); |
轉(zhuǎn)載于:https://www.cnblogs.com/ry123/archive/2013/04/24/3039720.html
總結(jié)
以上是生活随笔為你收集整理的JS高效关键字搜索---转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关注并订阅Autodesk地理信息解决方
- 下一篇: gcd学习