【发布时间】:2011-09-29 08:21:32
【问题描述】:
我想使用 jQuery 将特定单词(笔记本电脑、华硕、宏碁)替换为链接(test.com/l12、test.com/13、test.com/14)。 我找到了这个功能
<script type="text/javascript">
(function($) {
var thePage = $("body");
thePage.text(thePage.html().replace(/laptop/ig, '<a href="http://test.com/laptop">laptop</a>'));
})(jQuery)
</script>
但问题是它替换了现有 url 中的笔记本电脑单词(如果 url 是 test.com/laptop-review,则创建诸如 test.com/test.com/laptop-review 之类的 url)和所有笔记本电脑 html(html id , classes, urls etc) 被销毁。
谢谢
【问题讨论】:
-
replace(/^laptop/ig,...或者你可以试试replace(/laptop\s/ig,所以任何后面跟空格的单词都不能包含空格 -
从来没有想过这个。我会试试的
标签: jquery url replace word words