2016-06-26 40 views
-3

我需要一些关于JavaScript正则表达式的帮助。我需要一种方法来选择以下:用于HTML模板字符串的Javascript RegExp选择器

${user.name} 
${user.age} 

从下面的HTML:

<tr> 
    <td>${user.name}</td> 
    <td>${user.age}</td> 
</tr> 

可能有多个<td>在他们和字符串的“用户”部分不同的值并不总是将成为用户。它可能是任何东西。

理想情况下,我希望他们回来作为一个数组,虽然目前我会解决任何事情。

+0

是不是更好的方法,处理给定DOM片段的表格单元集合,而不是将整个片段作为字符串处理? –

回答

1

我不是正则表达式的专家,但我希望它能工作。

var str = "your html" 
var matches = str.match(/\${[a-zA-Z0-9.]+}/g); 

了给定的输入HTML,输出将是

["${user.name}", "${user.age}"] 
0

像这样的东西可能会帮助你:

str = str.match(/\$\{\w*\.\w*\}/g); 

示例见匹配Regex

var str = "<tr><td>${user.name}</td><td>${user.age}</td></tr>"; 
 
str = str.match(/\$\{\w*\.\w*\}/g); 
 
console.log(str)

说明:

    根据需要 *之间的零和无限次,多次地,用之于:
  • \$字符$字面上
  • \{字符{字面上
  • \w*匹配任何单词字符匹配[a-zA-Z0-9_]
    • 量词匹配[greedy]
  • \.符合字符.字面上
  • \w*匹配任何单词字符[a-zA-Z0-9_]
    • 量词:*之间的零和无限次,多次地,用之于需要[贪婪] \}匹配字符}字面上
  • g修饰符:全局。所有比赛(不要在第一场比赛中返回)
0

我写此功能为你的目的:

/** 
    * This function assumes html with template syntax as a argument 
    * and returns array from this marks. 
    * Example of usage: 
    * If you have html markup like this: 
    * <table> 
    * <tr> 
    *  <td>${user.name}</td> 
    *  <td>${user.age}</td> 
    * </tr> 
    * </table> 
    * 
    * and call function with document.body.innerHTML as a argument 
    * console.log(getTemplates(document.body.innerHTML.toString())); 
    * You will get the following result: 
    * Array [ "${user.name}", "${user.age}" ] 
    * 
    * @param input html string with template marks - ${variable.name} 
    * @returns {Array} - all marks 
    * @author Georgi Naumov 
    * [email protected] for contacts and suggestions. 
    */ 
    function getTemplates(input) { 
     return (input.match(/\$\{[^}\s]+\}/g) || []); 
    } 
    console.log(getTemplates(document.body.innerHTML.toString())); 

您将获得导致这种形式:

Array [ "${user.name}", "${user.age}" ] 
0
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`.