1. Write a JavaScript function and input a selector of the specified type (only three simple CSS selectors such as id, class, and tagName are supported, and there is no need to be compatible with combined selectors)
The problem is: I can’t understand the following regular matching,var reg = /^(#)?(.)?(w )$/img;var regResult = reg.exec(selector);,details as follows
var query = function(selector) { var reg = /^(#)?(\.)?(\w+)$/img; var regResult = reg.exec(selector); var result = []; //如果是id选择器 if(regResult[1]) { ... } //如果是class选择器 else if(regResult[2]) { ... } //如果是标签选择器 else if(regResult[3]) { ... } }
/^(#)?(.)?(w+)$/img
Tear it down //The part between // is the regular content and the following img is the regular matching method
i:ignorCase ignores case
m: mutiple allows Multi-line matching
g: global matching by glob means matching to the end of the target string
Regular content:
^(#)?(.)?(w+)$
^ means starting with xxx $ means ending with xxxx(#)? You can bring one or not #
(.)? You can bring it. Or None.
(w+) matches one or more words
/^(#)?(.)?(w+)$/img
(#)?
matching ID(.)?
matches className(w+)
The rest are tag names or specific ID or className names in [1,2]Xie Yao.
Please refer to @trionfo1993
Give you a dom fragment in utils I wrote