我的网址查询字符串设计不佳,我无法轻易改变,例如
我需要从中提取元素,例如45628
目前我正在使用
document.URL.split(/aff=|_/)[5];
但是我不喜欢这个解决方案,因为如果URL结构的其他部分发生了很大变化,那么我的解决方案就会破解
相反,我想说的是
分裂" aff ="然后拆分" _"
有没有一种简单的方法可以做到这一点,寻找JS答案
答案 0 :(得分:2)
Pretty sure you can do it like this:
document.URL.split("aff=")[1].split("_")[0];
答案 1 :(得分:0)
I would start by splitting the string into tokens, if you can. Rather than working with foo=bar&fin=bin
, break it down into [['foo', 'bar'], ['fin', 'bin]]
. You can do that by splitting on the &
and then the splitting each of those on the =
character:
const data = 'source=999&promotype=promo&cmpid=abc--dfg--hif-_-1234&cm=qrs-stv-_wyx&aff=45628_THIS+IS+Test_Example';
console.log(data.split('&').map(it => it.split('=')));
Next, take the tokens you want and extract the leading digits:
const data = 'source=999&promotype=promo&cmpid=abc--dfg--hif-_-1234&cm=qrs-stv-_wyx&aff=45628_THIS+IS+Test_Example';
const tokens = data.split('&').map(it => it.split('='));
const [key,val] = tokens.find(([key]) => key === 'aff');
console.log(key, val.match(/[0-9]+/));
答案 2 :(得分:0)
var re = new RegExp(/ aff =(\ d +)/); var ext = re.exec(url)[1]; 警报(EXT)