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

Notepad++正则替换:仅替换特定字符间的指定字符

只替换双引号内特定字符的解决方案

我明白你的痛点——只想修改双引号里的特定字符,外面的内容原封不动对吧?之前尝试没成功大概率是因为直接全局替换把引号外的内容也改了,或者没精准匹配到引号内的范围。下面给你两个实用的方案,完美支持多链接场景:

方案一:Python实现

核心思路是用正则匹配所有双引号内的内容,然后对每个匹配到的子串单独执行替换规则,最后再把处理后的内容放回引号中。

import re

def process_quoted_content(match):
    # 提取引号内的原始内容
    inner_content = match.group(1)
    # 按规则替换:空格转下划线,变音符号转对应字符
    inner_content = inner_content.replace(' ', '_')
    inner_content = inner_content.replace('ö', 'oe')
    inner_content = inner_content.replace('ä', 'ae')
    # 把处理后的内容重新包裹在双引号里返回
    return f'"{inner_content}"'

# 测试用的原始文本(支持多个链接)
original_html = '<a href="#foö bar foo bär">link</a><a href="#bär foo">another link</a>'
# 执行替换
processed_html = re.sub(r'"([^"]+)"', process_quoted_content, original_html)

print(processed_html)

执行后输出:

<a href="#foo_bar_foo_baer">link</a><a href="#baer_foo">another link</a>

方案二:JavaScript实现

逻辑和Python一致,利用正则的全局匹配和回调函数处理每个引号内的内容:

const originalHTML = '<a href="#foö bar foo bär">link</a><a href="#bär foo">another link</a>';

const processedHTML = originalHTML.replace(/"([^"]+)"/g, (fullMatch, innerContent) => {
    let updatedContent = innerContent.replace(/ /g, '_');
    updatedContent = updatedContent.replace(/ö/g, 'oe');
    updatedContent = updatedContent.replace(/ä/g, 'ae');
    return `"${updatedContent}"`;
});

console.log(processedHTML);

扩展说明

  • 如果需要处理更多变音符号(比如ü→ue、ß→ss),只需要在替换函数里添加对应的replace语句即可。
  • 如果你的文本里有转义的双引号(比如\"),可以把正则调整为"((?:\\.|[^"])+)",这样就能正确匹配包含转义引号的内容了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:12:36