あなたがすることができます:
$('a[href$="://www.mydomain.com/test/index.php?this_page=foo"]')
.attr('href', 'https://www.mydomain.com/test/index.php?this_page=bar');
attribute-ends-withセレクター[docs]はネイティブの CSS3 セレクターであるため、これらの要素の選択は非常に高速です。もちろん、http[s]
and などの部分に正規表現を使用することもできますが.filter
、これは遅くなる可能性があります (リンクの数にもよります)。
URL に、新しい URL の一部である必要がある他のパラメーターが含まれている場合は、各リンクを個別に処理する必要があります。
// Using the attribute-contains selector now. This still assumes that
// `this_page=foo` is the first parameter in the URL
var pattern = /^https?:\/\/www.mydomain.com/test/index.php?this_page=foo(&|$)/;
$('a[href*="://www.mydomain.com/test/index.php?this_page=foo"]')
.attr('href', function(i, val) {
return val.replace(pattern, 'https://www.mydomain.com/test/index.php?this_page=bar$1');
});
URL の任意の場所にある場合は、ベース URLthis_page
を含むすべてのリンクを取得できます。this_page=foo
これは、で始まる他のページ値がない場合にのみ機能しますfoo
。
var pattern_page = /([?&])this_page=foo(&|$)/;
$('a[href*="://www.mydomain.com/test/index.php?"][href*="this_page=foo"]')
.attr('href', function(i, val) {
return val.replace('http:', 'https:')
.replace(pattern_page, '$1this_page=bar$2');
});