gpt4 book ai didi

jquery - 使用 jquery 替换特定文本

转载 作者:行者123 更新时间:2023-11-27 23:16:34 25 4
gpt4 key购买 nike

这是html格式的文本

...now have strategies to |lead our company| successfully...

这就是我想要使用 jquery 的目的

...now have strategies to <strong>lead our company</strong> successfully...

我试过了

jQuery(function(){ 
jQuery('.quotes').each(function() {
console.log(jQuery(this).text());
var text = jQuery(this).text().replace(/[|]/g,"<strong>");
var texts = jQuery(text).replace(/[|]/g,"</strong>");
jQuery(this).html(text);
});

});

最佳答案

您可以像这样使用带有全局修饰符的正则表达式:

\|([^|]+)\|

替换为:

'<strong>$1</strong>'

| 是正则表达式中的特殊字符,可以转义 (\|) 以按字面意义引用它。

([^|]+) 捕获包含至少一个非 | 字符的组。

var text = '...now have strategies to |lead our company| successfully...';
text = text.replace(/\|([^|]+)\|/g, '<strong>$1</strong>');
console.log(text);

关于jquery - 使用 jquery 替换特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42828589/

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