为什么以下正则表达式不删除 ## 字符

Why isn't the following regex removing the ## characters?

本文关键字:删除 字符 正则表达式 为什么      更新时间:2023-09-26
  for (var i = 0; i < tree.length; ++i) {
    if (tree[i].match(/^##/g)) {
      console.log(tree[i])
      tree[i] = '<p><a href="#toc-' + tocIndex++ + '">' +
        tree[i].replace('/^## /gm', '') +
      '</a></p>'
      console.log(tree[i])
      tocItems.push(tree[i])
    }
  }

console.log(tree[i])输出## Chapter 1第二个输出:

<p><a href="#toc-1">## Chapter 1</a></p>

但它应该输出:

<p><a href="#toc-1">Chapter 1</a></p>

我做错了什么?

你没有正确使用正则表达式 在谷歌上搜索如何在 JavaScript 中使用正则表达式来学习这一点。 顺便说一句,你不需要正则表达式来做到这一点。 这是修复:

 tree[i].replace('##', '') + '</a></p>'