我有一個腳本,通過搜索網址和尋找一個特定的域名,然後追加一些參數到網址的末尾。但是,如果運行多次,它會將重複參數添加到同一個網址。是否有可能檢查網址是否已經有參數?追加參數到URL,避免重複
代碼被修改
<div id="wp-content-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content"><a title="Link to test domain" href="http://www.domain.com">Link to google</a>
<a href="http://google.com/directory/subdirectory/index.html">This is another link</a>
<a href="http://domain.com/directory/index.html">This is a 3rd link</a>
<a href="http://www.domain.com/subdir?parameter">this url already has parameters</a></textarea></div>
jQuery腳本
var url = 'www.domain.com';
var append = '?parameter';
$(".wp-editor-area").each(function() {
$(this).text(urlify($(this).text()));
});
function urlify(text) {
var urlRegex = /(\b(https?|ftp|file):\/\/[www.domain.com][-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(urlRegex, function(url) {
return url + append;
})
}
電流輸出
<a title="Link to test domain" href="http://www.domain.com?parameter">Link to google</a>
<a href="http://google.com/directory/subdirectory/index.html">This is another link</a>
<a href="http://domain.com/directory/index.html?parameter">This is a 3rd link</a>
<a href="http://www.domain.com/subdir?parameter?parameter">this url already has parameters</a>
理想的輸出不會在URL^
感謝的末尾添加第二個「?參數」!
是不是隻是檢查參數字符串是否在完整的url或不是?如果是的話就不要添加它,否則加上參數 – Huangism 2012-07-11 20:32:25
是的,那就是他想要的。 – james31rock 2012-07-12 13:15:25