* I've already searched stack overflow and tried answers for all of the similar questions *
I'm writing a node script that:
- Scans a directory for html files
- Reads the file contents into a string
- Searches the string for an id or class name
I'm using a regex to find the id or class name. I'm able to get a match when I'm searching for an id, but not when I'm searching for a class name.
Heres my example code:
https://jsbin.com/bayuquzulu/edit?js,console
For simplicity, I put the html into a variable and omitted the file read portion.
var html = '<section class="mb-4" id="button-test">'+
'<button type="button" class="btn btn-primary">Primary</button>'+
'<button type="button" class="btn btn-secondary">Secondary</button>'+
'<button type="button" class="btn btn-success">Success</button>'+
'</section>';
var scan = function(content) {
var props = ['btn-primary', 'button-test'], res = {};
for (var i = 0; i < props.length; i++) {
var prop = props[i];
var exps = [
{key: '#', exp: new RegExp("<*.* id=(["]|')\s*"+prop+"\s*(["]|').*>", "gi")},
{key: 'data-comp=', exp: new RegExp("<*.* data-dna=(["]|')\s*"+prop+"\s*(["]|').*>", "gi")},
{key: '.', exp: new RegExp("<*.* class=(["]|')*."+prop+".*(["]|').*>", "gi")}
];
for (var e = 0; e < exps.length; e++) {
var item = exps[e];
var key = item.key;
var exp = item.exp;
var match = content.match(exp);
key += prop;
if (match) {
if (!res[key]) { res[key] = []; }
res[key].push(match[0]);
}
}
}
return res;
}
console.log(scan(html));
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…