You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript正则替换XML中字符串的指定出现项(适配多行)

Fixing Multi-Line Regex Replacement for Specific Instance in XML String

Got it, let's work through this problem step by step. Your original code struggles with multi-line text and targeting the exact 2nd occurrence of <SUNDAY/>—here's how to fix it properly.

What's Off With the Original Approach?

Your current regex has a few critical issues:

  • Invalid syntax like $\g which isn't a valid regex construct.
  • Trying to match n consecutive instances and replace the last one fails when there's other content (newlines, other tags) between the target elements.
  • It doesn't handle multi-line text gracefully, since the regex logic relies on line endings which break across your XML structure.

The Reliable Solution: Count Matches in a Replace Callback

Instead of forcing a complex regex to target the nth match directly, use JavaScript's String.replace() with a global regex and a callback function to count matches as they're found. This works perfectly for both single-line and multi-line text.

Here's the revised code:

// Your multi-line XML data (formatted with newlines for readability)
let xmlData = `<?xml version="1.0" encoding="utf-8"?>
<RecipeData>
  <SUNDAY Value="1"/>
  <SUNDAY/>
  <SUNDAY/>
  <SUNDAY/>
  <MONDAY Value="1"/>
  </MONDAY>
  <MONDAY>
  </MONDAY>
</RecipeData>`;

const targetPattern = /<SUNDAY\s*\/\s*>/g; // Matches <SUNDAY/> with optional whitespace
const targetInstance = 2; // Replace the 2nd occurrence
let matchCount = 0;

const modifiedXml = xmlData.replace(targetPattern, (match) => {
  matchCount++;
  // Replace only when we hit the desired instance number
  return matchCount === targetInstance ? '<HHHH/>' : match;
});

console.log(modifiedXml);

How This Works:

  1. Global Regex: The /g flag ensures we find all occurrences of <SUNDAY/>—including variants with extra spaces like <SUNDAY / >.
  2. Callback Counting: Every time the regex finds a match, the callback increments a counter. When the counter hits your target number (2), it returns the replacement text <HHHH/>; otherwise, it keeps the original match.
  3. Multi-Line Friendly: This approach doesn't care about line breaks because the global regex scans the entire string, not just individual lines.

Exact Desired Output:

Running this code will produce exactly what you're looking for:

<?xml version="1.0" encoding="utf-8"?>
<RecipeData>
  <SUNDAY Value="1"/>
  <HHHH/>
  <SUNDAY/>
  <SUNDAY/>
  <MONDAY Value="1"/>
  </MONDAY>
  <MONDAY>
  </MONDAY>
</RecipeData>

内容的提问来源于stack exchange,提问作者piyush

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 17:49:11