使用正则表达式匹配并替换href/src属性值的方案咨询
Got it, let's figure out how to replace the entire src or href attributes in your HTML elements—specifically those that contain a 32-bit GUID in their values. Here are two solid approaches depending on your environment and needs:
DOM parsing is the safest way to handle HTML because it properly understands the structure of tags and attributes, avoiding edge cases that regex might miss.
浏览器端 JavaScript 实现
If you're working directly in a browser or with client-side JS:
// Update all img tags with src containing a 32-bit GUID document.querySelectorAll('img').forEach(img => { const guidRegex = /[0-9A-F]{32}/i; // Check if the src attribute contains a 32-bit GUID if (guidRegex.test(img.getAttribute('src'))) { // Replace the entire src attribute with your new value img.setAttribute('src', 'https://your-new-image-url.com'); } }); // Update all a tags with href containing a 32-bit GUID document.querySelectorAll('a').forEach(link => { const guidRegex = /[0-9A-F]{32}/i; if (guidRegex.test(link.getAttribute('href'))) { link.setAttribute('href', 'https://your-new-link-url.com'); } });
This code targets every img and a tag, checks if their attribute contains a 32-character hexadecimal GUID, and replaces the full attribute value if a match is found.
后端 Python 实现(用 BeautifulSoup)
For server-side processing of HTML strings, use BeautifulSoup:
from bs4 import BeautifulSoup import re # Your original HTML content html = '''<img src="/~/media/75F8BA07F3BC4C3F91A71D6A049E6BD4.ashx" alt="" /> <a href="/~/link.aspx?_id=2CD5F3FBD0334A7DA7CB81F9520BEED5&_z=z">Some text</a>''' soup = BeautifulSoup(html, 'html.parser') # Replace img src attributes with GUIDs for img in soup.find_all('img', src=re.compile(r'[0-9A-F]{32}', re.IGNORECASE)): img['src'] = 'https://your-new-image-url.com' # Replace a href attributes with GUIDs for link in soup.find_all('a', href=re.compile(r'[0-9A-F]{32}', re.IGNORECASE)): link['href'] = 'https://your-new-link-url.com' # Get the modified HTML print(soup.prettify())
BeautifulSoup uses regex to filter elements with GUID-containing attributes, then overwrites the attribute directly.
If your HTML is consistently formatted (no weird edge cases like escaped quotes in attributes), regex can work for quick string replacements.
JavaScript 正则替换示例
const originalHtml = '<img src="/~/media/75F8BA07F3BC4C3F91A71D6A049E6BD4.ashx" alt="" /> <a href="/~/link.aspx?_id=2CD5F3FBD0334A7DA7CB81F9520BEED5&_z=z">Some text</a>'; // Replace img src attributes const updatedHtml = originalHtml .replace(/(<img[^>]+src=")[^"]*[0-9A-F]{32}[^"]*(")/gi, '$1https://your-new-image-url.com$2') // Replace a href attributes .replace(/(<a[^>]+href=")[^"]*[0-9A-F]{32}[^"]*(")/gi, '$1https://your-new-link-url.com$2'); console.log(updatedHtml);
Note: Regex can break if your HTML has unexpected formatting (e.g., single quotes for attributes, line breaks inside tags). Use this only when you're sure the HTML structure is rigid.
内容的提问来源于stack exchange,提问作者brother

