(五)JS學習筆記 - Sizzle選取器

來源:互聯網
上載者:User

標籤:

Sizzle詞法解析

sizzle對於分組過濾處理都用正則,其中都有一個特點,就是都是元字元^開頭,限制匹配的初始,所以tokenize也是從左邊開始一層一層的剝離。

•可能會應用到正則如下:

// 空白var whitespace = "[\\x20\\t\\r\\n\\f]";// 匹配\後任一字元,字母或數字或-,ascii值非\x00-\xa0範圍內的字元var characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+" var identifier = characterEncoding.replace( "w" , "w#" )  // 匹配關係符> + ~var rcombinators = new RegExp( "^" + whitespace + "*([>+~]|" + whitespace + ")" + whitespace + "*" )  // 匹配=[非‘非"]var rattributeQuotes = new RegExp( "=" + whitespace + "*([^\\]‘\"]*?)" + whitespace + "*\\]" , "g" )  

 

•正則組合:

///^#((?:\\.|[\w-] | [^\x00-\xa0] ) +)/var ID = new RegExp("^#(" + characterEncoding + ")")

匹配ID,例如:$(‘#id‘)

var TAG =  new RegExp( "^(" + characterEncoding.replace( "w", "w*" ) + ")" );

匹配標籤,例如:$(‘input‘)

var Class = new RegExp( "^\\.(" + characterEncoding + ")" );

匹配class名,例如:$(‘.test‘)

attributes = "\\[" + whitespace + "*(" + characterEncoding + ")" + whitespace +               "*(?:([*^$|!~]?=)" + whitespace + "*(?:([‘\"])((?:\\\\.|[^\\\\])*?)\\3|(" + identifier + ")|)|)" + whitespace + "*\\]"
匹配屬性選取器,例如:[arr^=‘val‘]   擷取的群組1:characterEncoding :匹配例子中arr   擷取的群組2:([*^$|!~]?=):匹配例子中^=   擷取的群組3:[‘\"]:匹配例子中‘   擷取的群組4:(?:\\\\.|[^\\\\])*? 解釋一下這個外層捕獲\和後面的任意數量字元或非\字元,內層有?:是不捕獲的,匹配例子中的val   擷取的群組5:匹配identifier,匹配[id=#a]的情況 
pseudos = ":(" + characterEncoding + ")(?:\\((([‘\"])((?:\\\\.|[^\\\\])*?)\\3|((?:\\\\.|[^\\\\()[\\]]|" + attributes.replace( 3, 8 ) + ")*)|.*)\\)|)"  
匹配偽類運算式,例如li:nth-child(2n+1)   擷取的群組1:characterEncoding :匹配例子中nth-child   擷取的群組2:匹配例子中的2n+1   擷取的群組3:匹配‘ or "   擷取的群組4:匹配\和後面任何字元或非\字元   擷取的群組5:匹配不再‘ or "內的\和後面任一字元或非\()[]字元或attributes

(五)JS學習筆記 - Sizzle選取器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.