What you have is structured data in the form of a URL. You can use plain JavaScript without regex to parse and traverse it:
- Parse as search parameters.
- Get the parameter
url
.
- Split the result into parts.
- Get the part after
/dp/
.
function extractASIN(url) {
const searchParams = new URLSearchParams(url);
const queryURL = searchParams.get("url");
const parts = queryURL.split("/");
const precedingIndex = parts.indexOf("dp");
return parts[precedingIndex+1];
}
test("www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_next_aps_sr_pg2_1?ie=UTF8&adId=A00689312QGP01ZB3KM3N&url=%2FLifebuoy-Alcohol-Based-Protection-Sanitizer%2Fdp%2FB0866JTZXN%2Fref%3Dsr_1_49_sspa%3Fdchild%3D1%26keywords%3DHand%2Bsanitizer%26qid%3D1622721840%26smid%3DAT95IG9ONZD7S%26sr%3D8-49-spons%26psc%3D1&qualifier=1622721840&id=1588289180417395&widgetName=sp_atf_next");
test("www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_aps_sr_pg1_1?ie=UTF8&adId=A027625218II94KXW8EWJ&url=%2FDettol-Sanitizer-Regular-200-Pack%2Fdp%2FB076ZH5ZJL%2Fref%3Dsr_1_2_sspa%3Fdchild%3D1%26keywords%3DHand%2Bsanitizer%26qid%3D1622720663%26sr%3D8-2-spons%26psc%3D1&qualifier=1622720663&id=6679001242725581&widgetName=sp_atf");
function test(str) {
const result = extractASIN(str);
console.log(`ASIN = ${result}`);
}
Alternatively, you can use basic string manipulation
- Get where
dp%2F
is.
- Where the next
%2F
is.
- Extract the string between the two.
function extractASIN(url) {
const startToken = "dp%2F";
const endToken = "%2F";
const start = url.indexOf(startToken) + startToken.length;
const end = url.indexOf(endToken, start);
return url.slice(start, end);
}
test("www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_next_aps_sr_pg2_1?ie=UTF8&adId=A00689312QGP01ZB3KM3N&url=%2FLifebuoy-Alcohol-Based-Protection-Sanitizer%2Fdp%2FB0866JTZXN%2Fref%3Dsr_1_49_sspa%3Fdchild%3D1%26keywords%3DHand%2Bsanitizer%26qid%3D1622721840%26smid%3DAT95IG9ONZD7S%26sr%3D8-49-spons%26psc%3D1&qualifier=1622721840&id=1588289180417395&widgetName=sp_atf_next");
test("www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_aps_sr_pg1_1?ie=UTF8&adId=A027625218II94KXW8EWJ&url=%2FDettol-Sanitizer-Regular-200-Pack%2Fdp%2FB076ZH5ZJL%2Fref%3Dsr_1_2_sspa%3Fdchild%3D1%26keywords%3DHand%2Bsanitizer%26qid%3D1622720663%26sr%3D8-2-spons%26psc%3D1&qualifier=1622720663&id=6679001242725581&widgetName=sp_atf");
function test(str) {
const result = extractASIN(str);
console.log(`ASIN = ${result}`);
}
If you really want a regex, you can use /dp%2F(\w*)%2F/
to capture the value between dp%2F
and %2F
then return it:
function extractASIN(url) {
return url.match(/dp%2F(\w*)%2F/)[1];
}
test("www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_next_aps_sr_pg2_1?ie=UTF8&adId=A00689312QGP01ZB3KM3N&url=%2FLifebuoy-Alcohol-Based-Protection-Sanitizer%2Fdp%2FB0866JTZXN%2Fref%3Dsr_1_49_sspa%3Fdchild%3D1%26keywords%3DHand%2Bsanitizer%26qid%3D1622721840%26smid%3DAT95IG9ONZD7S%26sr%3D8-49-spons%26psc%3D1&qualifier=1622721840&id=1588289180417395&widgetName=sp_atf_next");
test("www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_aps_sr_pg1_1?ie=UTF8&adId=A027625218II94KXW8EWJ&url=%2FDettol-Sanitizer-Regular-200-Pack%2Fdp%2FB076ZH5ZJL%2Fref%3Dsr_1_2_sspa%3Fdchild%3D1%26keywords%3DHand%2Bsanitizer%26qid%3D1622720663%26sr%3D8-2-spons%26psc%3D1&qualifier=1622720663&id=6679001242725581&widgetName=sp_atf");
function test(str) {
const result = extractASIN(str);
console.log(`ASIN = ${result}`);
}