JavaScript RegEx:在两个"标记"之间替换字符串中的文本

enl*_*loz 3 javascript regex string replace

我需要这个:

输入

<div>some text [img]path_to_image.jpg[\img]</div>
<div>some more text</div>
<div> and some more and more text [img]path_to_image2.jpg[\img]</div>
Run Code Online (Sandbox Code Playgroud)

产量

<div>some text <img src="path_to_image.jpg"></div>
<div>some more text</div>
<div>and some more and more text <img src="path_to_image2.jpg"></div>
Run Code Online (Sandbox Code Playgroud)

这是我的尝试,也失败了

var input  = "some text [img]path_to_image[/img] some other text";
var output = input.replace (/(?:(?:\[img\]|\[\/img\])*)/mg, "");
alert(output)
//output: some text path_to_image some other text
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

Py.*_*Py. 6

一个正则表达式

var output = input.replace (/\[img\](.*?)\[\/img\]/g, "<img src='$1'/>");
Run Code Online (Sandbox Code Playgroud)

应该做

那么你的测试结果就是 some text <img src='path_to_image'/> some other text