日期:2014-05-16 浏览次数:20418 次
text.replace(new RegExp('(<a[^>].*>.*)('+keyword+')(.*</a>)'),'$1<b>$2</b>$3')
------解决方案--------------------
<script type="text/javascript">
var text = "<a href='http://www.baidu.com'>http://www.baidu.com</a>";
var keyword = "www";
var pattern = new RegExp('^(<[^>]+>[^<]*)(' + keyword + ')([^<]*<[^>]*>)$');
text = text.replace(pattern, "$1<b>$2</b>$3");
alert(text);
</script>
------解决方案--------------------
var text = "<a href='http://www.baidu.com'>http://www.baidu.com</a>afefeeaaa<A href='http://www.code-design.cn'>http://www.code-design.cn</a>"; var keyword = "www"; var rx = /(<a[^>]+>)([^>]+)(<\/a>)/ig text = text.replace(rx, function ($0, $1, $2, $3) { return $1 + $2.replace(keyword, '<b>' + keyword + '</b>') + $3; }); alert(text)