Clavianus Juneardo
Clavianus Juneardo

Reputation: 115

Finding substring with regex Javascript

How can I find the specific URL for specific tags if I have this categoryIdList and scripts, is it possible to get the specific url?

const categoryIdList = ["#travel", "#lifestyle", "#fnb", "#gadget_entertainment", "#dailyneeds", "#others_promo"]

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");
        });
});`

The result I want is like

{
  "#travel": "ajax.promolainnya.php?product=0&subcat=1",
  "#lifestyle": "ajax.promolainnya.php?product=0&subcat=2",
  // etc
}

How can I get that result using regex? Or any method in javascript

Thanks

Upvotes: 0

Views: 60

Answers (2)

Ben
Ben

Reputation: 5646

With the caveat that this is an exceptionally fragile approach and relies on a 1:1 mapping of the #id to uri in the .load method, a regex that will help you out relies on capture groups, and might look something like this:

/\$\(\"#.*\"\)\.click|\.load\(\".*\"\);/g

See a breakdown of what it's doing here: https://regex101.com/r/hD0zR5/7

You can use that regex in a script to turn a set of matches into an array, with every 2 array items being a key/value pair.

As long as you can guarantee that will be true (every 2 array items are a key/value pair) you can loop through the array and create they object you're looking for.

Since you already have the categoryIdList you could use those values to verify the keys when you're creating your object, if desired.

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)

Upvotes: 2

Flink
Flink

Reputation: 1006

I think what you are generally looking for is this Regex: "(.*?)" (https://regex101.com/r/jY8NBp/1)

This will get you every bit between quotation marks, you can then ignore the middle one when creating your object.

Upvotes: 1

Related Questions