更改(文档).ready上受正则表达式影响的字符串的颜色

所以我想在这里完成的是改变受某些正则表达式影响的单词的颜色。

$(document).ready(function(){ container = $("#modal_container section").text(); hashtags = /\#\w+/g; var container_hash = container.match(hashtags); $(container_hash).css({'color':'red'}); //We're grabbing the words, but how can I change the color? alert(container_hash); }); 

这是一个小提琴http://jsfiddle.net/vuDzC/2/ ,以使其更清晰。

谢谢

您无法更改textNodes / texts的颜色,您应该使用元素包装匹配的文本,然后设置该元素的样式。

 $("#modal_container section p").html(function(_, html) { return html.replace(/(\#\w+)/g, '$1'); }); 

http://jsfiddle.net/vuDzC/4/

  $(function(){ var container = $("#modal_container section"); var htmlWrappedHashtags = container.html().replace(/\(#\w+)/g, '$1'); container.html(htmlWrappedHashtags); $(".red").css({'color':'red'}); // or better: include ".red {color: red;}" into your CSS }); 

你正在寻找这种类型(或者应该是一个足够好的例子):