如何编写正则表达式来匹配 Donorbox EmbedForm?

How to write regex to match Donorbox EmbedForm?

我需要构建一个 Regex 规则,该规则必须验证值是否与 Donorbox 嵌入表单匹配。这是为了确保用户输入的代码确实来自 Donorbox。

Donorbox EmbedForm 示例:

<script src="https://donorbox.org/widget.js" paypalExpress="false"></script>
<iframe src="https://donorbox.org/embed/..." allowpaymentrequest="" frameborder="0"></iframe>

正则表达式需要知道它们是否是脚本和 iframe 标签,然后查看两个 src 标签以验证域。

到目前为止我做了什么:

<textarea (change)="donorBoxEmbedFormVerification(e)" type="text"></textarea>
        
donorBoxEmbedFormVerification(embedHtml: string):boolean {
   // need some help
   console.log(embedHtml.match('<\s*script[^>]*>(.*?)<\s*/\s*script>'));
   return true;
}

如能提供有关正则表达式的任何帮助,我们将不胜感激,

此致,

萨曼莎

您似乎想要匹配所有这样的 HTML 表单元素,其中包含 src 属性,其中 donorbox 作为子字符串。对于这种情况;你可以试试:

^(?=.*src="https:\/\/donorbox\.org.*).*

上面正则表达式的解释:

  • ^ - 表示行的开始。
  • (?=.*src="https:\/\/donorbox\.org.*) - 表示正向预测断言 src 属性等于 https://donorbox.org.
  • .* - 贪婪地 匹配所有内容零次或多次。

Regex Demo