编辑代码

// 设置标准输入接口
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  const exp = "(^|$|[,+])";
  const str = await readline();
  let isCorrect = true;
  let ans = [];
  //   let isOpen = false;
  let stack = [];
  for (let i = 0; i < str.length; i++) {
    const c = str[i];
    if (stack.length) isCorrect = false;
    if (c == "[") {
      stack.push(c);
      ans.push(c);
    } else if (c == "]") {
      if (stack.length) stack.pop();
      ans.push(c);
    }
    if (c == "_") {
      if ((i - 1 >= 0 && str[i - 1] == "\\") || !isCorrect) ans.push(c);
      else ans.push(exp);
    } else {
      ans.push(c);
    }
  }
  console.log(ans.join(""));
})();