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
时间: 2024-10-23 01:30:02