Javascript 在两个其他字符之间替换同一字符的多次出现

Javascript 在两个其他字符之间替换同一字符的多次出现,javascript,regex,Javascript,Regex,仅当字符位于两个特定的其他字符之间时,如何替换该字符?即使前后都有文字 例如,如果我有这样一个字符串: var text = "For text `in between two backticks, replace all #es with *s`. It should `find all such possible matches for #, including multiple ### together`, but shouldn't affect ### outside backtick

仅当字符位于两个特定的其他字符之间时,如何替换该字符?即使前后都有文字

例如,如果我有这样一个字符串:

var text = "For text `in between two backticks, replace all #es with *s`. It should `find all such possible matches for #, including multiple ### together`, but shouldn't affect ### outside backticks."
我期望的输出是:

"For text `in between two backticks, replace all *es with *s`. It should `find all such possible matches for *, including multiple *** together`, but shouldn't affect ### outside backticks."
我有以下代码:

text = text.replace(/`(.*?)#(.*?)`/gm, "`$1*$2`");

使用一个简单的
/`[^`]+`/g
正则表达式,该正则表达式将匹配反勾号,然后是反勾号以外的1+个字符,然后再次使用反勾号,并替换回调中的
\

var text=“对于文本‘在两个倒勾之间,将所有的#es替换为*s`。它应该‘找到#的所有可能匹配项,包括多个###在一起’,但不应该影响###外部倒勾。”;
var res=text.replace(/`[^`]+`/g,函数(m){
返回m.replace(/#/g,'*');
});

控制台日志(res)
那么您想在两个
*
之间替换
#
吗?不,我想在两个反勾(`)之间替换它