Sitecore技术问询:如何用正则参数准确检测触发FatalError的script标签
Let's break down what's going wrong here and how to fix it:
The Core Problem
Your current regex ^<script[^>]*>$ uses ^ and $ anchors, which tell the regex engine to match the entire string exactly as a single opening <script> tag (like <script> or <script src="foo.js">).
But here's the contradiction:
- You expect
<script>Test</script>to trigger an error, but this string doesn't match your regex (it ends with</script>, not just>) - Worse,
Testis also triggering an error, which suggests your tool's matching logic might be reversed (e.g., it's throwing an error when the text doesn't match the pattern, instead of when it does)
Solutions Based on Your Exact Need
1. If you want to block any text containing a <script> tag (opening or full block)
If your goal is to flag any text that includes a script tag (even partial), drop the ^ and $ anchors. Use this regex:
<script[^>]*>
URL-encoded for your Parameters field:
Pattern=<script[^>]*>&Text=Field "script" is not a valid entry.&Result=FatalError
This will match any opening <script> tag in the text, so <script>Test</script> will trigger the error (as expected), and Test won't.
2. If you want to strictly block full <script>...</script> tag pairs
For tighter control (only flag complete script blocks), use a non-greedy regex to match from the opening tag to the first closing tag:
<script[^>]*>.*?</script>
URL-encoded:
Pattern=<script[^>]*>.*?</script>&Text=Field "script" is not a valid entry.&Result=FatalError
This avoids matching across multiple script tags if they exist, and only flags text that contains a full script block.
3. Double-check your tool's matching logic
If neither of the above fixes work, confirm whether your tool throws an error when the text matches the pattern or when it doesn't. If it's the latter (error on mismatch), you'll need a negative regex to ensure the text doesn't contain script tags:
^(?!.*<script[^>]*>.*?</script>).*$
This regex ensures the text has no full script blocks — if it does, it won't match, triggering the error.
内容的提问来源于stack exchange,提问作者Zakayen

