JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<a href="https://www.banggood.com/VIOFO-A119-1440P-160-Degree-Wide-Angle-Car-DVR-With-GPS-Black-p-1068196.html" class="btn_offer_block re_track_btn" target="_blank" rel="nofollow">banggood</a>
<br>
<br>
<br>
<a href="http://www.gearbest.com/robot-vacuum/pp_615997.html?test=1" class="btn_offer_block re_track_btn" target="_blank" rel="nofollow">gearbest</a>
JavaScript
var ref = {
"banggood":"BQ06121739480201506E",
"amazon":9876,
"gearbest":1234
}
jQuery('a.btn_offer_block').each(function(i, obj) {
var url = jQuery(this).attr("href");
var domain = extractHostname(url)[0];
var cleanUrl = extractHostname(url)[1];
var newUrl = checkUrl(cleanUrl,domain);
jQuery(this).attr("href",newUrl);
});
function checkUrl(url,domain) {
var myRef = url;
if(typeof ref[domain] !== "undefined") {
myRef += "?p=" + ref[domain];
}
return myRef;
}
function extractHostname(url) {
var hostname;
var cleanUrl = url;
//find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf("?") != "-1") {
cleanUrl = url.split('?')[0];
}
if (url.indexOf("://") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
//find & remove port number
hostname = hostname.split(':')[0];
//find & remove "?"
hostname = hostname.split('?')[0];
var domaiNname = hostname.split('.')[1]; //domain name
var urls = [domaiNname,cleanUrl] // clean URL
return urls;
}