Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 替换两个字符之间的多个字符串_Javascript_Regex_Replace - Fatal编程技术网

Javascript 替换两个字符之间的多个字符串

Javascript 替换两个字符之间的多个字符串,javascript,regex,replace,Javascript,Regex,Replace,它应该显示“这里被替换,替换那些**文本。” 注意-它应该处理**之间的动态字符串,而不仅仅是text1和text2您必须转义*,并在*之后使用?进行非贪婪匹配: const func=str=>str.replace(/\*\*.*?\*\*/g,'replaced'); log(func('这里是**text1**和**text2**替换那些**文本'); log(func('这里是**一些较长的文本****') str = “Here are \*\*text1\*\* and \*\*

它应该显示
“这里被替换,替换那些**文本。”


注意-它应该处理**之间的动态字符串,而不仅仅是text1和text2

您必须转义
*
,并在
*
之后使用
进行非贪婪匹配:

const func=str=>str.replace(/\*\*.*?\*\*/g,'replaced');
log(func('这里是**text1**和**text2**替换那些**文本');
log(func('这里是**一些较长的文本****')
str = “Here are \*\*text1\*\* and \*\*text2\*\* replace those **texts.”

console.log(str.replace(/\*\*.*\*\*/, ‘replaced’)); // Here are replaced texts.