When matching a class, jquery is used for a relatively clumsy method. Both the class name of the target node and the expression must be processed before they can be used.
Match = "" + match [1]. replace (// \/g, "") + ""; for (VAR I = 0, ELEM; (ELEM = curloop [I])! = NULL; I ++) {If (ELEM. classname & ("" + ELEM. classname + ""). replace (/[\ t \ n \ r]/g ,""). indexof (MATCH)> = 0 )){//*******************}}
If match is. AAA, it is okay to use only one filter. What if match is. AAA. BBB ?! Is there any faster way? Yes. We need to construct a strong regular expression so that it can match the class names we need at the same time, either one or multiple, regardless of order. Thanks to the experts in the Group, this regular expression will soon be solved (I am proud of my powerful think tank, so I don't need 51js, javaeye, blueidea ......).
<Ul> <li class = "aaa bbb"> compliant </LI> <li class = "bbb aaa"> </LI> <li class = "aaa ccc bbb"> Yes </LI> <li class = "AAA"> NO </LI> <li class = "BBB"> NO </LI> </ul>
For example, if we want to match these Li elements that have both AAA and BBB, we only need to convert the expression into a regular expression and perform test verification directly, you no longer need to process the classname on the element.
VaR src = "" Var escape =/([-. * +? ^ $ {} () | [\] \/\])/G "AAA. bbb ". replace (/[^.] +/g, function ($) {SRC + = '(? = [\ S] * (? : ^ | \ S) '+ $. Replace (Escape,' \ $1 ') + '(? : \ S | $) '}) var Reg = new Regexp (SRC ); vaR T1 = "aaa bbb" Var t2 = "bbb aaa" Var T3 = "aaa ccc bbb" Var t4 = "AAA" Var T5 = "BBB" alert (Reg. test (T1) // true alert (Reg. test (T2) // true alert (Reg. test (T3) // true alert (Reg. test (T4) // false alert (Reg. test (T5) // false
2011.1.27 better solution
var src = ". AAA. bbb ". replace (/([\/\ [\] \:])/g, '\ $1') // replace the special characters allowed in classname. replace (/\. ([^.] +)/g ,'(? = [\ S] * (?: ^ | \ S) $1 (?: \ S | $) '); // match var Reg = new Regexp (' ^ '+ SRC); // match the beginning with ^, otherwise, we will find n other locations (n is the string length) if they do not match) by abcdvar T1 = "aaa bbb" Var t2 = "bbb aaa" Var T3 = "aaa ccc bbb" Var t4 = "AAA" Var T5 = "BBB" alert (Reg. test (T1) // truealert (Reg. test (T2) // truealert (Reg. test (T3) // truealert (Reg. test (T4) // falsealert (Reg. test (T5) // false