Notepad++正则替换含RTRD的a标签,保留文本内容
Alright, I see where you went wrong here—your current regex is targeting the text inside the <a> tag instead of the URL in the href attribute, which is why it's not picking up the links you want to modify. Let's fix this step by step.
Why Your Original Regex Fails
The pattern <a[^>]*>(RTRD+)</a> looks for <a> tags where the inner text contains consecutive RTRD characters. But your goal is to target tags where the href URL includes RTRD, so this regex doesn't match your intended cases at all.
Correct Regex for Your Use Case
We need to adjust the pattern to check for RTRD inside the href attribute, then capture the inner text to keep it.
Basic Version (for double-quoted href values)
- Find what:
<a[^>]*href="[^"]*RTRD[^"]*"[^>]*>([^<]+)</a> - Replace with:
$1
Let's break down how this works:
<a[^>]*: Matches the start of the<a>tag and any leading attributes beforehrefhref="[^"]*RTRD[^"]*": Targets thehrefattribute specifically, ensuring it contains theRTRDstring (the[^"]*parts match any characters except quotes to cover the full URL)[^>]*>: Matches any remaining attributes in the opening<a>tag, up to the closing>([^<]+): Captures the inner text of the<a>tag (this is what we want to keep—e.g.,TESTER)</a>: Matches the closing tag
Universal Version (supports both single and double quotes)
If your HTML uses single quotes for href values (e.g., <a href='something.RTRD.html'>...</a>), use this more flexible pattern:
- Find what:
<a[^>]*href=['"][^'"]*RTRD[^'"]*['"][^>]*>([^<]+)</a> - Replace with:
$1
How to Use This in Notepad++
- Open your HTML file in Notepad++
- Press
Ctrl+Hto open the Replace window - Under "Search Mode", select Regular Expression (make sure "
.matches newline" is unchecked—most<a>tags are single-line) - Paste your chosen "Find what" and "Replace with" patterns
- Click "Replace All" to modify all matching links, or "Find Next" to verify matches one by one before replacing
Test Your Examples
- Input:
<a href="http:\something.RTRD.html">TESTER</a>→ Output:TESTER - Input:
<a href="http:\something.RTRB.html">LEAVE</a>→ No match, stays unchanged
内容的提问来源于stack exchange,提问作者JA1

