gpt4 book ai didi

javascript - 从 char 到 char 的正则表达式选择由字符分隔的间隔

转载 作者:行者123 更新时间:2023-12-02 17:55:16 25 4
gpt4 key购买 nike

我在 JavaScript/JQuery 中有一个字符串,我只想替换不在 HTML 标记中的值:

< div class="myClass contentTab" id="myId">
this is my content and I will work only on this
</div>

我想将单词内容替换为 <b> + content + </b>,但不是 HTML 标记中的单词。 。结果应该是这样的:

< div class="myClass contentTab" id="myId">
this is my <b>content</b> and I will work only on this
</div>

div 的类 contentTab不应该被替换,而应该替换“内容”一词。

最佳答案

您可以简单地使用以下代码:

var temp = "<div class='myClass contentTab'id='myId'>  this is my content and I will   work only on this </div>";
$('body').append($(temp));
var req = temp.match(/>(.*?)</g);
var beforeresult = temp.match(/>(.*?)</g);
var afterresult = beforeresult[0].replace("content","<b>content</b>");
var aftertemp = temp.replace(beforeresult,afterresult);
$('body').append($(aftertemp));

引用这个 fiddle :

Fiddle

关于javascript - 从 char 到 char 的正则表达式选择由字符分隔的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21005726/

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