Source:
Http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html
Http://www.etherdream.com/funnyscript/Keyword/Keyword.html
For multi-keyword, large text matching, if the keyword only one, it is only the simplest string matching (match one), does not show the role.
varTreesearch ={maketree:function(strkeys) {"Use Strict"; varTblcur ={}, 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)) {//Generating child nodesTblcur =Tblcur[key]; } Else{tblcur= Tblcur[key] = {}; }} tblcur.end=true;//Last keyword does not have a separatorTblcur =Tblroot; } returnTblroot; }, Search:function(content, tblroot) {"Use Strict"; varTblcur, P_star= 0, N=Content.length, P_end, match,//If a match is foundMatch_key, Match_str, Arrmatch= [],//Store ResultsArrlength = 0//length index of the Arrmatch ; while(P_star <N) {tblcur= Tblroot;//backtrack to the rootP_end =P_star; Match_str= ""; Match=false; Do{Match_key=Content.charat (p_end); if(! (tblcur = Tblcur[match_key])) {//this match endsP_star + = 1; Break; }Else{match_str+=Match_key; } p_end+ = 1; if(Tblcur.end = = =true)//match to tail//Find match keyword{Match=true; } } while(true); if(Match = = =true) {//Maximum MatchArrmatch[arrlength] = {//Enhanced ReadabilityKey:match_str, Begin:p_star-1, end:p_end}; Arrlength+ = 1; P_star=P_end; } } returnArrmatch; }};
View Code
functionTest (strcontent, Strkeys) {varArrmatch, Tblroot=Treesearch.maketree (strkeys), T=NewDate (); Arrmatch=Treesearch.search (strcontent, tblroot); Console.log ("Time is:" + (NewDate ()-T) + "MM"); Console.log (Arrmatch);}vars = (function() { varThings = [' ', ' \ 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 ']; vars = ""; for(vari = 1000000; I >= 0; i--) {s+ = Things[parseint (Math.random () * things.length)%Things.length]}; returns;}) () test (s, ["ABC", "Efge", "Fun", "tree"]);
View Code
JavaScript Tens of thousands of keyword instant match--with the help of hash table fast matching algorithm