index.js 438 B

1234567891011121314151617
  1. const extractCodeBlocks = (str) => {
  2. if (!str) {
  3. return [];
  4. }
  5. // 正则表达式匹配所有的代码块内容
  6. const matches = str.match(/```[\s\S]*?\n([\s\S]*?)\n```/g);
  7. // 如果找到了匹配项,返回提取的内容,否则返回空数组
  8. return matches
  9. ? matches.map((match) =>
  10. match.replace(/```[\s\S]*?\n([\s\S]*?)\n```/, "$1").trim()
  11. )
  12. : [];
  13. };
  14. module.exports = { extractCodeBlocks };