2 回答
TA貢獻1806條經(jīng)驗 獲得超8個贊
需要注意的是,這是一種非常脆弱的方法,并且依賴于 .load 方法中#id到 uri 的 1:1 映射,因此可以幫助您的正則表達式依賴于捕獲組,并且可能如下所示:
/\$\(\"#.*\"\)\.click|\.load\(\".*\"\);/g
在此處查看其功能的細分:https://regex101.com/r/hD0zR5/7
您可以在腳本中使用該正則表達式將一組匹配項轉(zhuǎn)換為數(shù)組,其中每 2 個數(shù)組項是鍵/值對。
只要你能保證這是真的(每個2個數(shù)組項都是一個鍵/值對),你就可以遍歷數(shù)組并創(chuàng)建你要查找的對象。
由于您已經(jīng)擁有,因此如果需要,可以在創(chuàng)建對象時使用這些值來驗證鍵。categoryIdList
const scripts = `$(document).ready(function(){
$("#travel").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=0&subcat=1");
});
$("#lifestyle").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=0&subcat=2");
});
$("#fnb").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=0&subcat=3");
});
$("#gadget_entertainment").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=0&subcat=4");
});
$("#dailyneeds").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=0&subcat=5");
});
$("#others_promo").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=0&subcat=6");
});
$("#kartukredit").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=1");
});
$("#simpanan").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=2");
});
$("#others").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=3");
});
$("#ebanking").click(function(){
$("#contentpromolain2").load("ajax.promolainnya.php?product=4");
});
});`
const urls = {}
const matches = [...scripts.matchAll(/\$\(\"(#.*)\"\)\.click|\.load\(\"(.*)\"\);/g)]
for (let i = 0;i < matches.length;i += 2) {
urls[matches[i][1]] = matches[i + 1][2]
}
console.log(urls)
TA貢獻1836條經(jīng)驗 獲得超4個贊
我認為您通常正在尋找的是此正則表達式:(https://regex101.com/r/jY8NBp/1"(.*?)")
這將使您在引號之間獲得每個位,然后在創(chuàng)建對象時可以忽略中間的引號。
添加回答
舉報
