var
// \$\{ ... search for occurrence of template string opener sequence,
// ([^.]+) ... then memorize every character (at least one) that is not a dot,
// \. ... look for a single dot - the divider of your template string's sub-domains,
// ([^.]+) ... then again memorize every character that is not a dot,
// \} ... finally identify the template string delimiter sequence.
regXTemplateString = (/\$\{([^.]+)\.([^.]+)\}/),
// make array from any derived html table cell element collection.
tableCellList = Array.from(yourTableRowHTMLElement.getElementsByTagName('td')),
// reduce table cell array to a data structure that one can work with further
viewList = tableCellList.reduce(function (collector, tdElement) {
var
executedTemplate = regXTemplateString.exec(tdElement.innerHTML);
if (executedTemplate) {
collector.push({
templateElement : tdElement, // the html element reference
domainName : executedTemplate[1], // e.g. "user" from "${user.name}"
identifier : executedTemplate[2] // e.g. "age" from "${user.age}"
});
}
return collector;
}, []);
// do whatever you're pleased with `viewList`.
是不是更好的方法,處理給定DOM片段的表格單元集合,而不是將整個片段作爲字符串處理? –