这是一段 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/