用于HTML模板字符串的Javascript RegExp选择器

时间:2016-06-26 18:55:35

标签: javascript html regex templates

我的JavaScript正则表达式需要一些帮助。我需要一种方法来选择以下内容:

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

来自以下html:

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

可能有多个<td>,其中包含不同的值,并且&#39;用户&#39;字符串的一部分并不总是用户。它可以是任何东西。

理想情况下,我希望他们能够像阵列一样回归,尽管目前我还会满足于此。

4 个答案:

答案 0 :(得分:1)

我不是正则表达专家,但我希望它能奏效。

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

对于您的输入html,输出将是

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

答案 1 :(得分:0)

这样的事可能会帮助你:

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

请参阅匹配Regex

的示例

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

说明:

  • \$字面匹配字符$
  • \{字面匹配字符{
  • \w*匹配任何字符[a-zA-Z0-9_]
    • 量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪婪]
  • \.字面匹配字符.
  • \w*匹配任何字符[a-zA-Z0-9_]
    • 量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪婪] \}字面匹配字符}
  • g修饰符:全局。所有比赛(首场比赛都没有回归)

答案 2 :(得分: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
     * gonaumov@gmail.com for contacts and suggestions.
     */
    function getTemplates(input) {
        return (input.match(/\$\{[^}\s]+\}/g) || []);
    }
    console.log(getTemplates(document.body.innerHTML.toString()));

您将以此表格获得结果:

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

答案 3 :(得分: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`.