gpt4 book ai didi

JavaScript 正则表达式 : replace text in string between two "markers"

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:50 26 4
gpt4 key购买 nike

我需要这个:

输入

<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>

输出

<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>

这是我的尝试,也失败了

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

感谢您的帮助!

最佳答案

像这样的正则表达式

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

应该做

然后测试的输出是 some text <img src='path_to_image'/> some other text

关于JavaScript 正则表达式 : replace text in string between two "markers",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com