选择标签之间的innerHTML

时间:2017-02-06 21:35:35

标签: c# html regex tags

在HTML页面中,我需要逐个匹配所有innerHTML。

我制作一个允许匹配除innerHTML之外的所有标签的REGEX(包括BR标签),但我不能做相反的事情......

([<][^br][^<]*[>])

您可以在此网址上看到一个示例:https://regex101.com/r/h9tKHj/1

关于这个DOM:

<li class="product-faq-item">
          <p class="product-faq-title">{{XXXXXXXXXXXX1}}</p>
          <div class="product-faq-container">
            <p class="product-faq-text">{{XXXXXXXXXXXX2}}<br>
              {{XXXXXXXXXXXX3}}
            </p>
          </div>
        </li>

        <li class="product-faq-item">
          <p class="product-faq-title">{{XXXXXXXXXXXX4}}</p>
          <div class="product-faq-container">
            <p class="product-faq-text">{{XXXXXXXXXXXX5}}</p>
          </div>
        </li>

我的目标是恢复这个:

Match 1 : {{XXXXXXXXXXXX1}}
Match 2 : {{XXXXXXXXXXXX2}}
Match 3 : {{XXXXXXXXXXXX3}}
Match 4 : {{XXXXXXXXXXXX4}}
Match 5 : {{XXXXXXXXXXXX5}}

提前感谢您的帮助!

度过愉快的一天,

安东尼,

1 个答案:

答案 0 :(得分:0)

如果您想用值maby替换{{key}},请将其替换为:

var input = @"<li class='product-faq-item'>
      <p class='product-faq-title'>{{XXXXXXXXXXXX1}}</p>
      <div class='product-faq-container'>
        <p class='product-faq-text'>{{XXXXXXXXXXXX2}}<br>
          {{XXXXXXXXXXXX3}}
        </p>
      </div>
    </li>

    <li class='product-faq-item'>
      <p class='product-faq-title'>{{XXXXXXXXXXXX4}}</p>
      <div class='product-faq-container'>
        <p class='product-faq-text'>{{XXXXXXXXXXXX5}}</p>
      </div>
    </li>";

var regex = new Regex("{{.*?}}");
var dic = new Dictionary<string, object>();
dic["XXXXXXXXXXXX1"] = "X1Val";
dic["XXXXXXXXXXXX2"] = "X2Val";
dic["XXXXXXXXXXXX3"] = "X3Val";
dic["XXXXXXXXXXXX4"] = "X4Val";
dic["XXXXXXXXXXXX5"] = "X5Val";

var output = regex.Replace(input, match => $"{dic[match.Value.Replace("{", "").Replace("}", "")]}");