Efficient classname matching

Source: Internet
Author: User

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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.