使用jquery

时间:2017-03-16 08:17:20

标签: jquery html

这是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); 
   }); 

});

1 个答案:

答案 0 :(得分:2)

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

\|([^|]+)\|

取代:

'<strong>$1</strong>'

|是正则表达式中的一个特殊字符,可以对其进行转义(\|)以便按字面意思引用它。

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

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

相关问题