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

如何将Bulk Rename Utility的正则表达式转换为JavaScript代码?

Converting Bulk Rename Utility's RegEx Rename Logic to JavaScript

Hey there! No worries at all—let’s break this down step by step since you’re new to this. Converting Bulk Rename Utility’s (BRU) RegEx renaming logic to JavaScript is totally doable, and once you get the hang of it, you’ll have full control over customizing your renaming rules.

First, let’s recap how BRU’s RegEx renaming works:

  • You define a match pattern (RegEx) to capture parts of the original filename (like dates, prefixes, or serial numbers) using capture groups (...).
  • You define a replacement pattern that rearranges or modifies those captured groups (using $1, $2, etc., to reference each group).
  • BRU automatically preserves file extensions unless you explicitly include them in your pattern.

Basic Conversion Example

Let’s say in BRU you set up this rule:

Match pattern: ^(DOC)_(\d{2})_(\w{3})$
Replace with: $3-$1-$2

This would turn DOC_05_REP.pdf into REP-DOC-05.pdf. Here’s how to replicate this in JavaScript:

// Sample list of files to rename
const fileNames = ["DOC_05_REP.pdf", "DOC_12_SUM.docx", "DOC_08_REV.xlsx"];

// 1. Define your RegEx pattern (matches the filename part, not the extension)
const matchPattern = /^(DOC)_(\d{2})_(\w{3})/;

// 2. Define your replacement string (matches BRU's $1/$2 syntax)
const replacementPattern = "$3-$1-$2";

// 3. Process each file: split name/extension, apply rename, rejoin
const renamedFiles = fileNames.map(file => {
  // Split filename and extension (handles cases where extensions have dots like .tar.gz)
  const lastDotIndex = file.lastIndexOf(".");
  const originalName = lastDotIndex !== -1 ? file.slice(0, lastDotIndex) : file;
  const extension = lastDotIndex !== -1 ? file.slice(lastDotIndex) : "";

  // Apply the RegEx replacement
  const newName = originalName.replace(matchPattern, replacementPattern);

  return newName + extension;
});

console.log(renamedFiles);
// Output: ["REP-DOC-05.pdf", "SUM-DOC-12.docx", "REV-DOC-08.xlsx"]

Advanced: Custom Logic with Callback Functions

If you need more flexibility (like incrementing numbers, modifying text case, or conditional changes), JavaScript lets you use a callback function in replace() instead of a static string. For example, let’s modify the above to uppercase the captured group $3 and increment the number in $2:

const advancedRenamedFiles = fileNames.map(file => {
  const lastDotIndex = file.lastIndexOf(".");
  const originalName = lastDotIndex !== -1 ? file.slice(0, lastDotIndex) : file;
  const extension = lastDotIndex !== -1 ? file.slice(lastDotIndex) : "";

  const newName = originalName.replace(/^(DOC)_(\d{2})_(\w{3})/, (fullMatch, group1, group2, group3) => {
    // Increment the number by 1
    const incrementedNum = parseInt(group2) + 1;
    // Pad with leading zero to keep 2 digits
    const paddedNum = incrementedNum.toString().padStart(2, "0");
    // Uppercase the third group
    const upperGroup3 = group3.toUpperCase();

    return `${upperGroup3}-${group1}-${paddedNum}`;
  });

  return newName + extension;
});

console.log(advancedRenamedFiles);
// Output: ["REP-DOC-06.pdf", "SUM-DOC-13.docx", "REV-DOC-09.xlsx"]

Key Notes to Match BRU Behavior

  • Case Sensitivity: BRU has a "Case sensitive" checkbox—if you want to replicate that in JS, add the i flag to your RegEx (e.g., /pattern/i).
  • Whole Filename Match: BRU’s RegEx matches the entire filename (excluding extension) by default. If you want to match anywhere in the name, remove the ^ (start) and $ (end) anchors from your pattern.
  • Special Characters: If your filenames have spaces, parentheses, or other special characters, make sure to escape them in your RegEx (e.g., \( for () or use a non-greedy match if needed.

内容的提问来源于stack exchange,提问作者Santiago Cuartas Rmrz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:34:01