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

正则表达式左懒惰匹配:查找替换带GUID的特定HTML片段

如何用正则左懒惰(非贪婪)匹配并替换特定的Span包裹片段

Hey there! Let's break down how to solve this regex matching and replacement task you're working on. You need to target those [[<span ... field-id="GUID"> random text </span>]] code snippets, and using a lazy (non-greedy) match is key to avoid accidentally capturing multiple of these segments in one go.

Step 1: The Lazy Match Regex

Here's the regex pattern that will do exactly what you need:

\[\[<span[^>]+field-id="[^"]+"[^>]*>.*?</span>\]\]

Let's break down each part to understand how it works:

  • \[\[: Escapes the opening [[ (since square brackets are special characters in regex)
  • <span: Matches the start of the span tag
  • [^>]+: Greedily matches any characters except > to cover all attributes before the field-id
  • field-id="[^"]+": Targets the specific field-id attribute, matching the GUID inside the quotes (no quotes in GUID, so greedy here is safe)
  • [^>]*>: Matches any remaining attributes in the span tag until we hit the closing >
  • .*?: The critical lazy (non-greedy) match—this will grab any text inside the span, stopping at the first </span> it finds (instead of the last one, which a greedy .* would do)
  • </span>\]\]: Escapes and matches the closing </span>]] sequence

Step 2: Example Usage

Let's test this with your sample code:

LAbel
Label:  [[ some text ... ]]  [[ some other text ... ]]

This regex will only match the specific target snippet you mentioned:

[[<span href="#" style="background: red; color: white;" field-id="db983948-6458-4be8-9044-174093d39976"> some other text ... </span>]]

It won't accidentally include the first span segment because the lazy .*? stops at the first </span> it encounters.

Step 3: Implementation in Common Tools/Languages

JavaScript

const originalContent = 'Your full HTML content here';
const targetRegex = /\[\[<span[^>]+field-id="[^"]+"[^>]*>.*?<\/span>\]\]/g;
const replacementText = 'Your desired replacement content';
const updatedContent = originalContent.replace(targetRegex, replacementText);

Python

import re

original_content = 'Your full HTML content here'
target_regex = r'\[\[<span[^>]+field-id="[^"]+"[^>]*>.*?</span>\]\]'
replacement_text = 'Your desired replacement content'
updated_content = re.sub(target_regex, replacement_text, original_content)

VS Code Find/Replace

  • Open the Find/Replace panel (Ctrl+F then click the replace icon)
  • Enable regex mode (click the .* button)
  • Paste the regex in the Find field, your replacement in the Replace field
  • Use "Replace" or "Replace All" as needed

Quick Notes

  • If your span content includes line breaks, add the dotall flag:
    • In JavaScript: Add s to the regex (/.../gs)
    • In Python: Add re.DOTALL as a flag to re.sub()
  • This pattern works for any span tag with a field-id attribute wrapped in [[ and ]]—it doesn't care about other attributes like href or style.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:11:14