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

使用正则表达式匹配并替换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:

方法1:用DOM解析(推荐,更可靠)

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&amp;_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.

方法2:用正则表达式(适合简单、规范的HTML)

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&amp;_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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:37:16