str = “Here are \*\*text1\*\* and \*\*text2\*\* replace those **texts.”
console.log(str.replace(/\*\*.*\*\*/, ‘replaced’)); // Here are replaced texts.
它应该显示“Here are replaced and replaced replace those **texts.”
注意-它应该处理**之间的动态字符串,而不仅仅是text1和text2
答案 0 :(得分:0)
在*
之后,您必须转义?
并使用.*
进行非贪婪匹配:
const func = str => str.replace(/\*\*.*?\*\*/g, 'replaced');
console.log(func('Here are **text1** and **text2** replace those **texts.'));
console.log(func('Here is **some longer text** **'));