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

使用正则表达式批量移除HTML文件<div>标签内词尾感叹号

Got it, let's get this batch replacement sorted for your HTML files. Your regex is already on the right track—we just need to tweak it to capture the word portion separately, then pair it with tools that can handle multiple files at once. Here are a few practical, developer-friendly methods:

Method 1: Perl (Cross-Platform Command-Line)

Perl shines for regex-based batch file edits because it handles multi-line scenarios smoothly and supports in-place editing with backups. Use this command:

perl -pi.bak -e 's/(?-s)(<div>)*(\w+)(!)(?!\w*;)(?=[^<]*<\/div>)/$1$2/g' *.html

Quick Breakdown:

  • -pi.bak: The -p flag loops through every line of each file, -i.bak creates a backup of the original file (so you can revert if needed), and -e runs the replacement script.
  • We split your regex into capture groups: $1 catches the optional opening <div>, $2 grabs the word before the !, and $3 is the ! itself. Replacing with $1$2 drops the ! entirely while keeping the rest of the content intact.
  • To process files recursively in subdirectories, pair it with find:
find . -name "*.html" -exec perl -pi.bak -e 's/(?-s)(<div>)*(\w+)(!)(?!\w*;)(?=[^<]*<\/div>)/$1$2/g' {} +
Method 2: VS Code (Visual GUI Approach)

If you prefer a point-and-click interface, VS Code makes batch folder replacements a breeze:

  1. Open VS Code and use File > Open Folder to select your directory of HTML files.
  2. Press Ctrl+Shift+H (Windows/Linux) or Cmd+Shift+H (Mac) to launch the Replace in Folder panel.
  3. In the "Find" field, paste your adjusted regex: (?-s)(<div>)*(\w+)(!)(?!\w*;)(?=[^<]*</div>)
  4. In the "Replace" field, enter: $1$2
  5. Click the dropdown next to "Replace All" and select Replace All in Folder, then confirm the target directory to start the process.
Method 3: sed (Linux/macOS Command-Line)

sed works too, though you’ll need a small regex adjustment to fit its syntax:

sed -i.bak -E 's/(<div>)?([a-zA-Z0-9]+)(!)([^<]*<\/div>)/\1\2\4/g' *.html

Note:

This assumes your HTML uses simple, non-nested <div> tags. If you have nested divs, the regex might misfire since it stops at the first </div> it encounters.

Critical Pre-Check:

Always test the replacement on a single sample HTML file first before running it on all your files. This lets you confirm the regex works as expected and avoids accidental data loss.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:27:09