/**
* 将源文本中的标签替换为顺序编号的占位符
* @param {string} sourceText - 包含标签的源文本
* @param {string} regexPattern - 正则表达式模式(不含修饰符)
* @returns {string} - 转换后的文本,标签被替换为{1}{2}...等占位符
*/
function tagWrapper(sourceText, regexPattern) {
if (!sourceText || !regexPattern) return sourceText;
let counter = 1;
// 创建正则表达式,添加全局标志
const regex = new RegExp(regexPattern, 'g');
// 执行替换
return sourceText.replace(regex, (match) => {
return `{${counter++}}`;
});
}
// 示例用法
const sourceText = `
Attacking an aniimo depletes the yellow <style=Hint_BgL>BREAK meter</style> under its status bar. When it's <style=Hint_BgL>depleted</style>, the aniimo will enter a <style=Hint_BgL>broken state</style> and can be caught. Press <style=Hint_BgL>#kHud/Interact#z</style> to catch it.
`;
// 替换样式标签中的内容(包括标签本身)
const result = tagWrapper(sourceText, '(<[^\>]+>)|(#[A-Za-z]+)|(/[A-Za-z]+)');
console.log(result);
console