我认为您不需要在这种情况下使用正则表达式。
jQuery 为您提供了足够的工具来完成工作。
以下是我如何在某些链接占位符上完成“搜索和替换”功能:
鉴于以下 HTML -
<div class="details">
<div class="series"><a href="my_cool_page.php">text</a></div>
<fb:comments-count href="[PAGEURL]"></fb:comments-count>
</div>
<div class="details">
<div class="series"><a href="awseome_content.php">text</a></div>
<fb:comments-count href="[PAGEURL]"></fb:comments-count>
</div>
<div class="details">
<div class="series"><a href="interesting_stuff.php">text</a></div>
<fb:comments-count href="[PAGEURL]"></fb:comments-count>
</div>
这个 jQuery 可以解决问题 -
$(function(){
// this grabs all the div elements with the class "details"
$("div.details").each(function(index,elem){
// within each details <div>, we look for an <a> element, and
// we save its href value in currentURL
var currentURL = $(this).find('div.series > a').attr('href');
// again within the details <div>, we search for any element who's href
// property contains [PAGEURL] and replace is with the value
// we got from the series <a> tag.
$(this).find('*[href*="[PAGEURL]"]').attr('href',currentURL);
});
});
最终期望的结果应该是这样的 -
<div class="details">
<div class="series"><a href="my_cool_page.php">text</a></div>
<fb:comments-count href="my_cool_page.php"></fb:comments-count>
</div>
<div class="details">
<div class="series"><a href="awseome_content.php">text</a></div>
<fb:comments-count href="awseome_content.php"></fb:comments-count>
</div>
<div class="details">
<div class="series"><a href="interesting_stuff.php">text</a></div>
<fb:comments-count href="interesting_stuff.php"></fb:comments-count>
</div>
我根据您的问题对实际 HTML 进行了一项更改。href
财产(以及与此相关的任何财产)的价值应始终用引号引起来。
<fb:comments-count href=
" [PAGEURL]
"></fb:comments-count>