Javascript 替换HTML注释的内容,但不替换注释本身
我有以下标记文件,其中包含一系列HTML样式的注释:Javascript 替换HTML注释的内容,但不替换注释本身,javascript,regex,Javascript,Regex,我有以下标记文件,其中包含一系列HTML样式的注释: hello this is my file <!-- START COMMENT --> <!-- END COMMENT --> the rest of the file 你好,这是我的文件 文件的其余部分 有时注释标记为空,有时注释标记中包含内容: hello this is my file <!-- START COMMENT --> some content here <!-- EN
hello this is my file
<!-- START COMMENT -->
<!-- END COMMENT -->
the rest of the file
你好,这是我的文件
文件的其余部分
有时注释标记为空,有时注释标记中包含内容:
hello this is my file
<!-- START COMMENT -->
some
content
here
<!-- END COMMENT -->
the rest of the file
你好,这是我的文件
一些
内容
在这里
文件的其余部分
我正在尝试替换标记的内容,但要保持标记的完整性,以便它们仍然围绕着内容。我尝试了以下正则表达式,但它似乎也替换了标记,这不是我需要的:
data = data.replace(
/<!-- START COMMENT -->[\s\S]*?<!-- END COMMENT -->/g,
generatePlaceholders(response)
)
data=data.replace(
/[\s\s]*?/g,
generatePlaceholders(响应)
)
如何调整我的正则表达式,使其只替换内容?您可以进行替换,用替换文本重新插入注释。 如果将
和
放在匹配组中,则可以在替换组中内容的同时再次引用和插入它们(以$1
和$2
作为开始和结束)
const p=document.getElementById('content').innerHTML;
常量替换='新内容';
log(p.replace(/()[\s\s]*?()/,`1${replacement}$2`)代码>
你好,这是我的档案
一些
内容
在这里
文件的其余部分